打印机中的纸张停止 - Java打印问题

时间:2015-04-27 00:44:28

标签: java printing

此类用于打印文件。我有一个运行此代码的JButton(未在下面的代码中显示)并打开一个打印对话框。但是,当我点击打印时,它开始打印,但如果文本没有占用整个页面,它将停止并且不会从打印机中弹出。我错过了可能导致此问题的事情吗?

try{
        FileInputStream textStream;
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        textStream = new FileInputStream(testFileName);
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc test = new SimpleDoc(textStream, flavor, null);

        PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset);

        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

        if(services.length == 0){
            if(defaultService==null){
                System.out.println("NO PRINTERS");
            }else{
                DocPrintJob job = defaultService.createPrintJob();
                job.print(test, aset);
            }
        }else{

            PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset);


            if(service != null){
                DocPrintJob job = service.createPrintJob();
                job.print(test, aset);
            }
        }

    }catch (IOException | PrintException e){
        e.printStackTrace();
    }

0 个答案:

没有答案