java pdfbox printerjob错误缩放/页面格式
作者:互联网
我正在尝试使用pdfbox打印现有的pdf文件.目前,我正在通过Maven使用pdfbox 2.0.0 RC3.
这是我当前的代码:
PDDocument document = PDDocument.load(new File(myPdfFile));
PrinterJob job = PrinterJob.getPrinterJob();
if (job.printDialog()) {
job.setPageable(new PDFPageable(document));
job.print();
}
document.close();
为了进行测试,我使用Adobe Acrobat打印了一个测试pdf,并用几行代码打印了相同的pdf.
除边界外,其他一切正常.所有边框(页眉,页脚,左侧和右侧)都太小,而页脚太小了.
设置正确的缩放比例/格式时,在互联网上找不到我没有的魔术方法吗?
解决方法:
尝试使用pdfbox 2.0.0-RC3版本
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();
这是另一个版本
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();
标签:pdf,printing,format,pdfbox,java 来源: https://codeday.me/bug/20191027/1943650.html