PDFBOX Printing : Printed PDF contains Junk characters for Arabic text from the PDF

StackOverflow https://stackoverflow.com/questions/21511010

  •  06-10-2022
  •  | 
  •  

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();
      }
}
有帮助吗?

解决方案

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

section from scan of 1.8.4 print-out

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

section from scan of 2.0.0-SNAPSHOT print-out

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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top