Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java pdfbox printerjob wrong scaling / page format

I'm trying to print an existing pdf file with pdfbox. Currently I'm using pdfbox 2.0.0 RC3 through maven.

This is my current code:

PDDocument document = PDDocument.load(new File(myPdfFile));
PrinterJob job = PrinterJob.getPrinterJob();

if (job.printDialog()) {
    job.setPageable(new PDFPageable(document));
    job.print();
}

document.close();

For testing I printed a test pdf with Adobe Acrobat and the same pdf with the few lines of code. Everything works fine except for the borders. All borders (header, footer, left & right side) are to small and the footer is way too small.

Is there a magic method that I couldn't find in the world wide web for setting the right scaling/format?

like image 362
Mr.Tr33 Avatar asked Oct 15 '25 09:10

Mr.Tr33


1 Answers

Try this for pdfbox 2.0.0-RC3 version

PDDocument doc = PDDocument.load(new File("test.pdf"));
PDFPrintable printable = new PDFPrintable(doc, Scaling.SHRINK_TO_FIT);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(printable);
job.print();

Here is another version

PDDocument doc = PDDocument.load(new File("test.pdf"));
PrinterJob job = PrinterJob.getPrinterJob();

// define custom paper
Paper paper = new Paper();
paper.setSize(306, 396); // 1/72 inch
paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); // no margins

// custom page format
PageFormat pageFormat = new PageFormat();
pageFormat.setPaper(paper);

// override the page format
Book book = new Book();
// append all pages
book.append(new PDFPrintable(doc, Scaling.SHRINK_TO_FIT), pageFormat, doc.getNumberOfPages());
job.setPageable(book);

job.print();
like image 104
Davit Mumladze Avatar answered Oct 16 '25 21:10

Davit Mumladze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!