e700. the quintessential printing program using a printing service.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 41 行

TXT
41
字号
This example demonstrates a program that prints an image on the default print service. Since DocPrintJob.print() is not guaranteed to be synchronous, it is necessary to watch for a print job completion event before closing the input stream. 
    import java.io.*;
    import javax.print.*;
    import javax.print.attribute.*;
    import javax.print.attribute.standard.*;
    import javax.print.event.*;
    
    public class BasicPrint {
        public static void main(String[] args) {
            try {
                // Open the image file
                InputStream is = new BufferedInputStream(
                    new FileInputStream("filename.gif"));
    
                // Find the default service
                DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
                PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    
                // Create the print job
                DocPrintJob job = service.createPrintJob();
                Doc doc = new SimpleDoc(is, flavor, null);
    
                // Monitor print job events; for the implementation of PrintJobWatcher,
                // see e702 Determining When a Print Job Has Finished
                PrintJobWatcher pjDone = new PrintJobWatcher(job);
    
                // Print it
                job.print(doc, null);
    
                // Wait for the print job to be done
                pjDone.waitForDone();
    
                // It is now safe to close the input stream
                is.close();
            } catch (PrintException e) {
            } catch (IOException e) {
            }
        }
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?