PDFBOX Printing : Printed PDF contains Junk charac

2019-01-28 13:14发布

问题:

I have a PDF file containing Arabic text and a watermark. I am using PDFBox to print the PDF from Java. My issue is the PDF is printed with high quality, but all the lines with Arabic characters have junk characters instead. Could somebody help on this?

Code:

    String pdfFile = "C:/AresEPOS_Home/Receipts/1391326264281.pdf";
    PDDocument document = null;
    try {
    document = PDDocument.load(pdfFile);
    //PDFont font = PDTrueTypeFont.loadTTF(document, "C:/Windows/Fonts/Arial.ttf");
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setJobName(new File(pdfFile).getName());
    PrintService[] printService = PrinterJob.lookupPrintServices();
    boolean printerFound = false;
    for (int i = 0; !printerFound && i < printService.length; i++) {
        if (printService[i].getName().indexOf("EPSON") != -1) {
            printJob.setPrintService(printService[i]);
            printerFound = true;
        }
    }
    document.silentPrint(printJob);
    } 
    finally {

      if (document != null) {
     document.close();
      }
}

回答1:

In essence

Your PDF can properly be printed using PDFBox 2.0.0-SNAPSHOT but not using PDFBox 1.8.4. Thus, either the Arabic font in question requires a feature which is not yet supported in PDFBox up to version 1.8.4 or there was a bug in 1.8.4 which meanwhile has been fixed.

The details

Printing the OP's document using PDFBox 1.8.4 resulted in some scrambled output like this

but printing it using the current PDFBox 2.0.0-SNAPSHOT resulted in a proper output like this

In 2.0.0-SNAPSHOT the PDDocument methods print and silentPrint have been removed, though, so the original

document.silentPrint(printJob);

has to be replaced by something like

printJob.setPageable(new PDPageable(document, printJob));
printJob.print();


标签: pdf pdfbox