You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PDFBox 2.0.27导入OCG PDF时禁用图层复现问题求助

PDFBox 2.0.27及更早版本OCG显示问题与移除方案咨询

在PDFBox 2.0.27及更早版本中,将页面导入为PDFormXObject并通过PDPageContentStream绘制时,所有已禁用的可选内容组(OCG)会重新显示,这完全阻碍了分层PDF的相关工作。现询问是否有彻底移除OCG的方法,同时排查以下测试代码是否存在错误。

测试代码片段1

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

测试代码片段2

public void createFile2(File output, LayoutPage layoutPage, LocalServer server) throws IOException {
    if (!output.exists() && !output.createNewFile())
        throw new IOException();
    PDRectangle box = new PDRectangle(0f, 0f, 3000, 3000);
    PDPage page = new PDPage(box);
    PDDocument document = new PDDocument();
    document.addPage(page);
    PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.OVERWRITE, true, true);
    PDDocument tmpPDFDocument = PDDocument.load(server.pdf());
    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();
}

内容的提问来源于stack exchange,提问作者Guyard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 23:01:23