首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用java进行批量打印

用java进行批量打印
EN

Stack Overflow用户
提问于 2012-03-16 11:55:36
回答 2查看 3.3K关注 0票数 2

我在使用java打印几个html文档时遇到了问题。我需要应用程序,为所有可打印文件显示一个打印对话框(文件计数可能很大)。首先,我尝试使用standart java方法来完成这个任务:

代码语言:javascript
复制
if (Desktop.isDesktopSupported()){
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.PRINT))
        {
            try {
                File html1 = new File("c://file1.html");
                File html2 = new File("c://file2.html");
                desktop.print(html1);
                desktop.print(html2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

但是我看到了每个可打印文件的一个对话框,它不适合我。然后我尝试使用,但事实证明我的打印机不支持html文件的DocFlavor,我支持的DocFlavor列表如下所示:

代码语言:javascript
复制
image/gif; class="[B"
image/gif; class="java.io.InputStream"
image/gif; class="java.net.URL"
image/jpeg; class="[B"
image/jpeg; class="java.io.InputStream"
image/jpeg; class="java.net.URL"
image/png; class="[B"
image/png; class="java.io.InputStream"
image/png; class="java.net.URL"
application/x-java-jvm-local-objectref; class="java.awt.print.Pageable"
application/x-java-jvm-local-objectref; class="java.awt.print.Printable"
application/octet-stream; class="[B"
application/octet-stream; class="java.net.URL"
application/octet-stream; class="java.io.InputStream"

然后,我尝试将html文件打印为图像(png,我在画图中绘制),我的代码:

代码语言:javascript
复制
PrintRequestAttributeSet pras =
                    new HashPrintRequestAttributeSet();
            DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
            PrintRequestAttributeSet aset =
                    new HashPrintRequestAttributeSet();
            aset.add(MediaSizeName.ISO_A4);
            aset.add(new Copies(1));
            aset.add(Sides.ONE_SIDED);
            aset.add(Finishings.STAPLE);

            PrintService printService[] =
                    PrintServiceLookup.lookupPrintServices(flavor, pras);
            PrintService defaultService =
                    PrintServiceLookup.lookupDefaultPrintService();
            PrintService service = ServiceUI.printDialog(null, 200, 200,
                    printService, defaultService, flavor, pras);
            if (service != null) {
                try {
                    FileInputStream fis = new FileInputStream("c://test//test.png");
                    DocAttributeSet das = new HashDocAttributeSet();
                    Doc doc1 = new SimpleDoc(fis, flavor, das);

                    FileInputStream fis2 = new FileInputStream("c://test//test2.png");
                    DocAttributeSet das2 = new HashDocAttributeSet();
                    Doc doc2 = new SimpleDoc(fis2, flavor, das2);

                    DocPrintJob job1 = service.createPrintJob();
                    DocPrintJob job2 = service.createPrintJob();

                    try {
                        job1.print(doc1, pras);
                        job2.print(doc2, pras);
                    } catch (PrintException e) {
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

它工作得很好,但是从html转换到图像并不是一个简单的问题。我试着使用swing组件,实现可打印接口和使用眼镜蛇库,但是它要求在表单上显示文档,这对我来说并不重要,因为我需要以“静默”模式打印,而不需要打开文档。

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-12 09:35:49

最后,我选择了这样的方式:

  1. 将html文件转换为pdf。
  2. 在静默打印模式下使用PDFBox打印pdf文件。
代码语言:javascript
复制
List<PDDocument> docs = new ArrayList<PDDocument>();
try {
    docs.add(PDDocument.load("c://test/test.pdf"));
    docs.add(PDDocument.load("c://test/test2.pdf"));
} catch (IOException e) {
    e.printStackTrace();
}

try {
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.ISO_A4);
    aset.add(new Copies(1));
    aset.add(Sides.ONE_SIDED);
    aset.add(Finishings.STAPLE);

    PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    PrintService service = ServiceUI.printDialog(null, 200, 200,
            printService, defaultService, flavor, pras);

    if (service != null && !docs.isEmpty()) {
        for (PDDocument doc : docs) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintService(service);
            doc.silentPrint(printJob);
        }
    }
} catch (PrinterException e) {
    e.printStackTrace();
} finally {
    for (PDDocument doc : docs) {
        if (doc != null) {
            try {
                doc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2012-03-16 12:08:37

在这里,您可以找到通过java代码访问打印机的完整代码。

它提供了如下功能

代码语言:javascript
复制
1. cancel print job,
2. display print dialog,
3. print file etc..

http://anonsvn.icesoft.org//repo/icepdf/tags/icepdf-3.1.0/icepdf/viewer/src/org/icepdf/ri/common/PrintHelper.java

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9736752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档