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

Lia*_*sby 5 java pointers printdialog printers

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

    }
}
Run Code Online (Sandbox Code Playgroud)

此代码具有printDialog,并在打印机上按预期打印

第二个代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

}

打印没有打印对话框,这是我想要的,但这将打印到打印机的空白页.

现在可能的目标是只使用其中一个代码,但我提供了我尝试过的.我需要让代码1工作,但没有printerDialog.

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

我相信问题是代码一中传递的参数PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras);已经不再传递了

无论如何从PrintService service = ServiceUI.printDialog传递参数(null,200,200,printService,defaultService,DocFlavor.INPUT_STREAM.GIF,pras); 没有使用对话框进入打印机或是否有一种跳过对话框的方式,就好像用户点击了是?

首先为非常长的帖子道歉.希望任何人都可以提供帮助,或者给我一些建议.先感谢您

use*_*620 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);
    }
Run Code Online (Sandbox Code Playgroud)