You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by "Eriksson, Hakan" <er...@siemens.com> on 2012/09/11 17:27:45 UTC

Possible to scale / rotate PDF document using PDFBox when printing?

Hi all,

I'm trying to use PDFBox in order to be able to print PDF files from my java application. The problem is that the PDF files may be in more or less any format (A1 - A5) in landscape or portrait orientation and should be printed on various printers typically supporting A3 & A4 format. If I just try to print a landscape A2 document on a A4 printer I normally get a single paper with the upper left corner of the original sheet.

As I'm quite unfamiliar with printing from a program I'd appreciate some help in how to make this work properly.

My code so far:

.......
        private PrinterJob printJob = null;

        private boolean bFirstPrint = true;
        private boolean bPrintingWasCancelled = false;

.......

        private void printPDFFile(String filename) throws PrinterException, IOException {

                PDDocument document = null;

                try {
                        document = PDDocument.load(filename);

                        if (printJob == null) {
                                printJob = PrinterJob.getPrinterJob();
                        }

                        // Extract the filename without path to be the name of the printer job as it shows on the queue
                        String sJobName = filename.substring(filename.lastIndexOf("\\") + 1);

                        printJob.setJobName(sJobName);
                        printJob.setPageable(document);

                        if (bFirstPrint) {
                                bFirstPrint = false;

                                if (printJob.printDialog()) {
                                        document.silentPrint(printJob);
                                } else {
                                        bPrintingWasCancelled = true;
                                }
                        } else {
                                if (!bPrintingWasCancelled) {
                                        document.silentPrint(printJob);
                                }
                        }

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