在最新版本 2.0.30 PDFBox 中,我发现图像渲染和用于将页面渲染到对象的 LayerUtility 之间存在不一致。
我的应用程序将 PDF 文档转换为图像以在应用程序中使用。 在该过程结束时,我们希望打印出与初始 PDF 具有相同质量的文档。为此,我们获得一个 PdFormXObject,但是当我们将其添加到新的 PDF 文档时,注释会消失。
正如您在下面的函数中看到的,我们可以比较图像和克隆的 PDF 之间的结果。
如何向PDFormXObject添加注释或如何在图像处理中忽略注释,以便两个工具保持一致。 或者可能是我的代码中有错误?
public void pdfbox(File url, float dpi, int idPage) {
try {
url = new File("");
// Helper only for JavaFX user
url = new FileChooser().showOpenDialog(null);
// Transform a pdf to image
PDDocument document = PDDocument.load(url);
PDPage page = document.getPage(idPage);
PDFRenderer renderer = new PDFRenderer(document);
BufferedImage bim = renderer.renderImageWithDPI(idPage, dpi);
// Specific fonction to display a buffered image in a JavaFX window.
Outil.display(new ImageView(SwingFXUtils.toFXImage(bim, null)));
// Transform a pdf to a layer in a new PDF
PDDocument newDocument = new PDDocument();
PDPage newPage = new PDPage( new PDRectangle(0f, 0f, 2000, 2000));
newDocument.addPage(newPage);
LayerUtility utility = new LayerUtility(newDocument);
PDDocument PDFDocument2 = PDDocument.load(url);
PDFormXObject pdFormXObject = utility.importPageAsForm(PDFDocument2, 0);
PDPageContentStream stream = new PDPageContentStream(newDocument, newPage, true, false);
stream.drawForm(pdFormXObject);
stream.close();
File tmpFile = Files.createTempFile("test", ".pdf").toFile();
newDocument.save(tmpFile);
newDocument.close();
System.out.println("File : " + tmpFile);
// Open the PDF file if a default application is supported
if(Desktop.isDesktopSupported()) Desktop.getDesktop().open(tmpFile);
} catch (IOException e) {
e.printStackTrace();
}
}
我尝试将 PDFBox 更新为 2.0.30 版本。 我在 LayerUtility 或 PDFrenderer 中搜索了不同的选项,但没有任何结果。
PS:我不知道如何分享带注释的PDF文件给你?
LayerUtility
不传输页面注释。
要在没有注释的情况下进行渲染,请执行以下操作:
PDFRenderer renderer = new PDFRenderer(document);
renderer.setAnnotationsFilter(new AnnotationFilter()
{
@Override
public boolean accept(PDAnnotation annotation)
{
return false;
}
});