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


Reply via email to