如何在 Apache PDfBox 中设置页面缩放选项 [英] How to set Page Scaling option in Apache PDfBox
问题描述
在我的应用程序中,我使用 Apache PDFBox 来渲染 PDF 文件并进行静音打印.
In my app, I am using Apache PDFBox to render PDF file and to silent print that fine.
PDFBox 可以很好地渲染 PFD,但我面临着缩放 的问题.这里我想在打印 PDF 之前设置页面缩放.
PDFBox works fine for rendering the PFD but I am facing issue scaling comes to the picture. Here I want to set Page scaling before printing the PDF.
在 acrobat Reader 的打印弹出窗口中,有四种打印 PDF 的选项.1>适合2> 实际尺寸3> 缩小过大的页面4> 自定义比例
In acrobat reader's print popup, there are four options for printing the PDF. 1> Fit 2> Actual Size 3> Shrink over sized pages 4> Custom Scale
这里我想将页面缩放设置为实际大小.我如何使用 Apache POI 做到这一点??
Here I want to set page scaling to Actual Size. How can I do it using Apache POI ??
推荐答案
我遇到了同样的问题,我觉得做这件简单的事情太难了.尽管它并不理想,但我最终还是选择了以下方式 - 甚至不使用 PDFBox 打印.
I had this same problem and I felt like it was way too difficult to do this one simple thing. Even though it's not ideal, I eventually settled for the following - not even using the PDFBox printing.
以下代码将页面一次一张地转换为图像,并使用 java2d 调整大小并打印出来.
The following code converts the pages to images one at a time and uses java2d to resize and print them out.
PDDocument pdfdoc = PDDocument.load(pdfPane.pdfFile);
@SuppressWarnings("unchecked")
final List<PDPage> pdfPages = pdfdoc.getDocumentCatalog().getAllPages();
PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.setJobName(pdfPane.pdfFile.getName());
pjob.setPrintable(new Printable()
{
@Override
public int print( Graphics g, PageFormat pf, int page ) throws PrinterException
{
if (page > pdfPages.size())
return NO_SUCH_PAGE;
try
{
g.drawImage(pdfPages.get(page).convertToImage()
,(int)pf.getImageableX()
,(int)pf.getImageableY()
,(int)pf.getImageableWidth()
,(int)pf.getImageableHeight()
,null);
}
catch (IOException e)
{
LoggerUtil.error(e);
}
return PAGE_EXISTS;
}
});
pjob.print();
pdfdoc.close();
这篇关于如何在 Apache PDfBox 中设置页面缩放选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!