2

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();
      }
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
AR11980
  • 31
  • 7
  • Which pdfbox Version, what is your code, do you have an example pdf? – mkl Feb 02 '14 at 15:05
  • Thanks for comment : PDFBox Version : pdfbox-1.8.4 Following is my code : – AR11980 Feb 03 '14 at 11:52
  • Can you provide an example pdf to reproduce the issue? – mkl Feb 03 '14 at 15:09
  • Thanks mkl!.I am happy to provide example PDF,but I believe, I cant attach here. Can I send to your email or can you please provide some link where I can post. Thanks – AR11980 Feb 04 '14 at 06:06
  • Most often people share such samples here using their Google or drop box account. If that's not possible for you, you'll find an email address in my profile here. – mkl Feb 04 '14 at 06:24

1 Answers1

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

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();
mkl
  • 90,588
  • 15
  • 125
  • 265
  • Osome, Thanks mkl, I will get PDFBox 2.0.0-SNAPSHOT and try to test in my application. Thanks a ton again. – AR11980 Feb 06 '14 at 07:44
  • If you are content with an answer to one of your questions, please accept it (click on the mark left of the answer). – mkl Feb 06 '14 at 09:13
  • Hi mkl, PDFBox2.0.0-SNAPSHOT's minimum requirement is java1.6 , my application is on 1.5. Is there any way I can get this one compiled with 1.5.Gr8 if you can comments on this. Thanks – AR11980 Feb 09 '14 at 13:31
  • I'm afraid I don't know (At work we had been bound to 1.4 for quite a long time and directly switched to 1.6 from there. Thus, I have no real first-hand experience with 1.5 specific stuff). You might want to make this a question in its own right. – mkl Feb 09 '14 at 14:30
  • @mkl Hey, sorry to hijack this comment-thread... But care to explain exactly how to silent print? There is no such class as `PDPageable` in the 2.0 snapshot. I've tried creating a `PDFPrinter`, with the doc in the constructor, then calling `silentprint()` on it... Causing a StackOverflow, since I'm not on Mac OSX's main cocoa-thread (wat?) Wasn't the idea with silentPrint not to cause any GUI interactions etc. Appriciate the help. Regards – Robin Jonsson Feb 11 '15 at 14:46
  • @RobinJonsson *There is no such class as PDPageable in the 2.0 snapshot* - Well, there **was** such a class in the 2.0.0-SNAPSHOT one year ago... I'll try to find out how to do that using the current SNAPSHOT. – mkl Feb 11 '15 at 15:33
  • @RobinJonsson At first glance it looks like the `PDFPrinter` method `silentPrint` meanwhile indeed is the method to use for silent printing. I'm not on a Mac, though, so I cannot reproduce your issue. You may want to make that a question in its own right, providing some code and the stack trace. – mkl Feb 11 '15 at 16:10
  • @mkl Thanks mate, I think I'll do that. – Robin Jonsson Feb 11 '15 at 18:46