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

使用光栅PTR打印机在java中打印文件,java,printers,printdialog,pointers,Java,Printers,Printdialog,Pointers,我有两个使用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 flav

我有两个使用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);

    }
}
}

打印时没有打印对话框,这正是我想要的,但这会在打印机上打印空白页

现在我的目标是只使用这些代码中的一个,但我已经提供了我已经尝试过的代码。我需要有代码1工作,但没有打印对话框

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

我认为问题在于从
PrintService service=ServiceUI.printDialog(null、200、200、PrintService、defaultService、DocFlavor.INPUT\u STREAM.GIF、pras)在代码1中传递的参数不再被传递

是否存在从PrintService service=ServiceUI.printDialog(null、200、200、PrintService、defaultService、DocFlavor.INPUT\u STREAM.GIF、pras)传递参数的方法;在不使用对话框的情况下输入打印机,或者是否有一种方法可以跳过对话框,就好像用户单击了“是”


首先为这篇很长的文章道歉。希望有人能帮忙,或者给我一些建议。提前感谢

如果您的目标只是打印一个文件,那么有更简单的方法,例如使用

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

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);
    }

可以用这种方法指定打印机吗?不可以,但这是我解决同一问题的方法。您可以使用命令
rundll32 printui.dll、printuitery/y/q/n“printer name”
通过java编写批处理文件,然后让它执行。这将更改计算机上的默认打印机,并且Desktop.print命令使用计算机的设置,因此它将始终打印到当前设置为默认的打印机。用这样的方法执行。我可以问什么是getGoodBatchExecString吗?哦,对不起,这与答案不太相关。我从以前编写的程序中复制粘贴了它。基本上,我的方法得到了一个字符串,即文件路径,但它总是非常不稳定,不起作用,所以我写了另一个方法来修复它。我将修复我的示例以使其更加准确。虽然ODT是正确的,但是您可以检查操作系统是基于windows还是基于unix的,如果是基于unix的,那么您可以编写bash脚本来执行类似的操作,但过程将不同。
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);
    }