PDFBOX导入页面作为表单呈现隐藏的可选内容组



在PDFbox 2.0.27的最后一个版本和以前的版本中,当流绘制PDFormXObject时,所有禁用的可选内容组都会重新出现。

这个bug阻止了所有使用分层pdf的工作。有办法完全去除ocg吗?你有主意吗?

编辑:在一个新的pdf文档中,我添加了这个带有多个ocg的pdf。

https://www.pdfill.com/example/pdf_layer_new.pdf

所有ocg可见的结果(WeTransfer文件可用7天(:

https://wetransfer.com/downloads/6550dcb45764d65fe96945b36e5d5b7220221010104929/2f07fd

PDDocument tmpPDFDocument = PDDocument.load(server.pdf());
LayerUtility utility = new LayerUtility(tmpPDFDocument);
PDFormXObject mapPDF = utility.importPageAsForm(tmpPDFDocument, 0);
stream.drawForm(form);

编辑2:

我尝试最大限度地清理我的代码,在2.0.27版本中尝试了很多次之后,也许是我的错,你看到这里有错误吗?

结果:https://wetransfer.com/downloads/8d8f27f182c5b47bc7226acd5595dff620221011134625/97ea54

public void createFile2(File output, LayoutPage layoutPage, LocalServer server) throws IOException {
// Creation du fichier de sortie
if (!output.exists() && !output.createNewFile())
throw new IOException();
// initialise le format de la page pdf
PDRectangle box = new PDRectangle(0f, 0f, 3000, 3000);
PDPage page = new PDPage(box);
// creation du document pdf avec la page, le document doit rester ouvert
// pendant toute l'impression (ignore sonar recommendation)
PDDocument document = new PDDocument();
document.addPage(page);
// initialise le context
PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.OVERWRITE, true, true);
PDDocument tmpPDFDocument = PDDocument.load(server.pdf()); // file url to import here
LayerUtility utility = new LayerUtility(tmpPDFDocument);
utility.wrapInSaveRestore(tmpPDFDocument.getPage(0));
PDFormXObject mapPDF = utility.importPageAsForm(tmpPDFDocument, 0);
stream.drawForm(mapPDF);
stream.close();
document.save(output);
document.close();
}
```

纠正

LayerUtility utility = new LayerUtility(tmpPDFDocument);

LayerUtility utility = new LayerUtility(document);

LayerUtility必须使用要修改的文档进行初始化。

最新更新