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?
David Mumladze
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();
来源:https://stackoverflow.com/questions/35479613/java-pdfbox-printerjob-wrong-scaling-page-format