使用光栅PTR打印机在java中打印文件 [英] Printing files in java with raster PTR printer

查看:209
本文介绍了使用光栅PTR打印机在java中打印文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个使用java打印的代码,如下所示:

I have two peices of code for printing using java as seen below:

第一个代码

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

    }
}

此代码有printDialog并打印按照预期的打印机

This code has a printDialog and prints as intended on the printer

第二个代码:

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

}

打印没有打印对话框,这是我所追求的,但这将打印到打印机的空白页。

prints without a print dialog which is what i am after but this will print a blank page to the printer.

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

Now may aim is to use only one of these codes but i have supplied what i have tried. I need to have code 1 working but with no printerDialog.

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

if i do remove the printerDialog from code 1 then basically it does the same the same as code 2 (prints blank on this printer).

我认为问题在于来自的代码一中传递的参数PrintService service = ServiceUI.printDialog(null,200, 200,printService,defaultService,DocFlavor.INPUT_STREAM.GIF,pras); 不再传递

I beleive the issue is with the parameters passed in code one from PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras); not been passed anymore

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

Is there anyway of passing the parameters from PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras); into the printer without using a dialog or is there a way of skipping the dialog as though the user has clicked yes?

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

Apologise firstly for the VERY long post. Hope anyone can help, or give me some advise. Thank you in advance

推荐答案

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

If your goal is just to print a file, there are much easier methods of doing so, such as using java.awt.Desktop.print

继承人执行下面提到的批处理文件的代码以获得更好的格式

heres the code to execute the batch file i mentioned below for better format

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

这篇关于使用光栅PTR打印机在java中打印文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆