Silent Printing of PDF From Within Java

可紊 提交于 2019-11-30 00:00:17

Apache PDFBox. It is currently in incubation, but the PDF printing functionality has been around before that. Internally, it uses the Java Print Services to create a print job, and it also supports silent printing.

Do note that it requires Fontbox as well, and the current (upcoming 0.8.0 release) has included graceful fallback for documents with Type 0 fonts. Type 1 fonts are printed correctly; however in 0.7.3, attempts to print documents with Type 0 fonts will result in an exception being thrown.

Maybe I'm misunderstanding, but why not just use the Print Service API directly? The following works for me (assumes you have the PDF document as a byte array):

DocFlavor flavor = DocFlavor.BYTE_ARRAY.PDF;
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
if (services.length > 0)
{
    DocPrintJob printJob = services[0].createPrintJob();
    Doc document = new SimpleDoc(pdfBytes, flavor, null)
    printJob.print(document, null);
}
else
{
    System.out.println("No PDF printer available.");
}

This works for me:

public void print() {
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
    FileInputStream psStream = null;  
    try {  
        psStream = new FileInputStream("c:\\test.pdf");  
        } catch (FileNotFoundException ffne) {  
          ffne.printStackTrace();  
        }  
    if (psStream == null) {  
        return;  
    }       
    if (services.length > 0)
    {
        PrintService myService = null;
        for(PrintService service : services) {
            System.out.println(service.getName());
            if(service.getName().contains("my printer")) {
                myService = service;
                break;
            }
        }
        DocPrintJob printJob = myService.createPrintJob();
        Doc document = new SimpleDoc(psStream, flavor, null);
        try {
            printJob.print(document, null);
        } catch (PrintException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else
    {
        System.out.println("No PDF printer available.");
    }       
}
RED SOFT ADAIR

Have a look at www.pdflib.com. Its comercial but PDFlib Lite is available for free for open source projects. It has bindings for java.

There is an example using JPedal at http://www.jpedal.org/support_egSP.php

You will need the commercial version of IcePdf if you want full font support.

I have experience with making Acrobat (Reader or Full) do the printing, but it's anything but silent (it is unattended, though - just depends on how 'silent' the silent requirement is). If there's interest, I can shoot you the native code that makes the required DDE calls.

iText is intended for creating PDF files (per a post I saw from the author), and thus probably isn't what you want.

I've used Qoppa's jPDFPrint quite successfully for exactly this purpose, but it's not cheap. If you can afford it, it's the most robust solution I've found thus far. I've also been very impressed with the level of support; they even generated some custom sample code for me.

I tried PDFBox, but found that it doesn't support the "Shrink to printable area" page scaling that you get with Acrobat. Not everyone will care about this feature, but it's essential for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!