java呼叫印表機

lvzhou_MadSky發表於2014-05-20

/**
         * 列印(rt.jar  javax print )
         */
        public static void print(){
             PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
                PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
                PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
                PrintService service = ServiceUI.printDialog(null, 200, 200, pservices,
                        defaultService, flavor, aset);
                if (service != null) {
                    try {
                        DocPrintJob pj = service.createPrintJob();
                        aset.add(MediaSizeName.ISO_A4);
                        FileInputStream fis = new FileInputStream("D:\\WinPrinter.txt");
                        DocAttributeSet das = new HashDocAttributeSet();
                        Doc doc = new SimpleDoc(fis, flavor, das);
                        pj.print(doc, aset);
                        Thread.sleep(10 * 1000);
                    } catch (FileNotFoundException fe) {
                        fe.printStackTrace();
                    } catch (PrintException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("列印失敗");
                }
        }

相關文章