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

孤街醉人 提交于 2019-11-28 09:43:27

问题


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


来源:https://stackoverflow.com/questions/21511010/pdfbox-printing-printed-pdf-contains-junk-characters-for-arabic-text-from-the

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