使用光栅PTR打印机在Java中打印文件

5
我有两段用Java打印的代码,如下所示:
第一段代码:
for(int i = 0; i < files.length; i++) {
    String file = "C:\\images\\colour\\"+files[i].getName();
    String filename = file;
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras);

    if (service != null) {
        DocPrintJob job = service.createPrintJob();

        PrintJobListener listener = new PrintJobAdapter() {
            public void printDataTransferCompleted(PrintJobEvent e) {
                System.exit(0);
            }
        };

        job.addPrintJobListener(listener);
        FileInputStream fis = new FileInputStream(filename);
        DocAttributeSet das = new HashDocAttributeSet();
        Doc doc = new SimpleDoc(fis, flavor, das);
        job.print(doc, pras);

    }
}

这段代码有一个打印对话框,并且可以按照预期在打印机上打印。 第二段代码:
try {
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    pras.add(new Copies(1));

    PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);

    if (pss.length == 0) 
        throw new RuntimeException("No printer services available.");

    PrintService ps = pss[3];
    System.out.println("Printing to " + ps);
    DocPrintJob job = ps.createPrintJob();

    FileInputStream fin = new FileInputStream(files[i]);
    Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
    job.print(doc, pras);

    fin.close();
}
catch (IOException ie) {
    ie.printStackTrace();
}
catch (PrintException pe) {
    pe.printStackTrace();
}

打印不带打印对话框,这就是我想要的,但这会将一个空白页面打印到打印机上。

现在我的目标是只使用其中一个代码,但我已经提供了我尝试过的内容。我需要使代码1工作,但没有printerDialog。

如果我从代码1中删除printerDialog,那么基本上它与代码2相同(在此打印机上打印空白)。

我认为问题出在从PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras);传递给代码一的参数不再被传递了

有没有办法将PrintService服务= ServiceUI.printDialog(null、200、200、printService、defaultService、DocFlavor.INPUT_STREAM.GIF、pras)中的参数传递到打印机而不使用对话框,或者有没有一种方式可以跳过对话框,就像用户点击了“是”一样?

首先非常抱歉发了这么长的帖子。希望任何人能帮助我或给我一些建议。谢谢您提前。

1个回答

0

如果您的目标只是打印文件,那么有更简单的方法可以实现,例如使用java.awt.Desktop.print

以下是执行我之前提到的批处理文件以获得更好格式的代码:

Process p;
    String execBatchPath = "your file path";

    try {
        p = Runtime.getRuntime().exec("cmd /c start " + execBatchPath);
        p.waitFor();
    } catch (IOException e) {
        FileIO.writeLog("IO exception while trying to run the batch");
        ErrorUtils.manageCatch(e);
    } catch (InterruptedException e) {
        FileIO.writeLog("Batch process was interrupted");
        ErrorUtils.manageCatch(e);
    }

这个方法能否指定打印机? - Liam Sorsby
我可以问一下getGoodBatchExecString是什么吗? - Liam Sorsby
哦,抱歉,那并不是答案相关的内容。我只是从之前写过的一个程序中复制粘贴了它。基本上我的方法得到的是一个文件路径字符串,但它总是很奇怪,不能正常工作,所以我又写了另一个方法来修复它。我会修改我的示例,使其更准确。 - user2464620
很高兴能够帮忙。祝好运。我认为你会发现这是一种更好的方法。 - user2464620
1
tbodt是正确的,但是你可以检查操作系统是否基于Windows或Unix,如果是Unix,则可以编写一个Bash脚本来完成类似的事情,但是过程会有所不同。 - user2464620
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接