Why isn't PDFBox printing a document using landscape orientation?

2.7k Views Asked by At

I have a problem printing a landscape document (doc.width > doc.height). I have no problem with portrait. I'm using the PDFBox 1.8.2 library. It prints the document in portrait even if I set landscape mode manually like in this blog. There are several closed JIRA bugs about this problem in the PDFBox project. How do I print landscape?

Note: I've added extra PDF library tags to flag users of those libraries to see if this problem exists in those libraries.

Code from the blog post:

Version : PdfBox-1.7.1 

PDDocument doc = PDDocument.load(psStream,true);

if (printService != null) {             

    PrinterJob pj =  PrinterJob.getPrinterJob();
    pj.defaultPage();
    pj.setCopies(Integer.parseInt(finalnumberOfCopies));
    pj.setPrintService(printService);
    pj.setPageable(new MyPDPageable(doc,pj));

    // don't use doc.silentPrint(pj) but pj.print() ...
    pj.print();
}
1

There are 1 best solutions below

1
Ric Glines On

This example at grepcode.com worked for me. To summarize, it first rotates a portrait page 90 degrees to get the landscape orientation, and then uses the concatenate2CTM method to similarly rotate your contentStream.