java pdfbox printerjob wrong scaling / page format

徘徊边缘 提交于 2019-12-07 15:20:36

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!