java - 使用 itext 编辑 pdf 时出现异常

标签 java pdf itext ecm redaction

我在尝试使用 itext 编辑 pdf 文档时遇到异常。 这个问题非常零星,有时它可以工作,有时它会抛出错误。

at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.access$6100(PdfContentStreamProcessor.java:60)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor$Do.invoke(PdfContentStreamProcessor.java:991)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpContentOperator.invoke(PdfCleanUpContentOperator.java:140)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.invokeOperator(PdfContentStreamProcessor.java:286)
at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.processContent(PdfContentStreamProcessor.java:425)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpProcessor.cleanUpPage(PdfCleanUpProcessor.java:160)
at com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpProcessor.cleanUp(PdfCleanUpProcessor.java:135)
at RedactionClass.tgestRedactJavishsInput(RedactionClass.java:56)
at RedactionClass.main(RedactionClass.java:23)

我用来编辑的代码如下:

public static void testRedact() throws IOException, DocumentException {

    InputStream resource = new FileInputStream("D:/itext/edited_120192824_5 (1).pdf");
    OutputStream result = new FileOutputStream(new File(OUTPUTDIR,
            "aviteshs.pdf"));

    PdfReader reader = new PdfReader(resource);
    PdfStamper stamper = new PdfStamper(reader, result);
    int pageCount = reader.getNumberOfPages();
    Rectangle linkLocation1 = new Rectangle(440f, 700f, 470f, 710f);
    Rectangle linkLocation2 = new Rectangle(308f, 205f, 338f, 215f);
    Rectangle linkLocation3 = new Rectangle(90f, 155f, 130f, 165f);
    List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
    for (int currentPage = 1; currentPage <= pageCount; currentPage++) {
        if (currentPage == 1) {
            cleanUpLocations.add(new PdfCleanUpLocation(currentPage,
                    linkLocation1, BaseColor.BLACK));
            cleanUpLocations.add(new PdfCleanUpLocation(currentPage,
                    linkLocation2, BaseColor.BLACK));
            cleanUpLocations.add(new PdfCleanUpLocation(currentPage,
                    linkLocation3, BaseColor.BLACK));
        } else {
            cleanUpLocations.add(new PdfCleanUpLocation(currentPage,
                    linkLocation1, BaseColor.BLACK));
        }
    }
    PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations,
            stamper);
    try {
        cleaner.cleanUp();
    } catch (Exception e) {
        e.printStackTrace();
    }
    stamper.close();
    reader.close();

}

由于客户文档,我无法共享它,试图找出一些相同的测试数据。

请在此处查找文档:

https://drive.google.com/file/d/0B-zalNTEeIOwM1JJVWctcW8ydU0/view?usp=drivesdk

最佳答案

简而言之: 此处出现 NullPointerException 的原因是 iText 不支持从其显示的页面继承表单 XObject 资源。根据 PDF 规范,此构造已过时,但在遵循早期 PDF 引用而不是规范的 PDF 中可能会遇到这种情况。

原因

相关文档的第 1 页包含 4 个名为 I1M0P1Q0 的 XObject 资源:

RUPS screenshot

正如您在屏幕截图中看到的,Q0 特别没有自己的资源 字典。但它的最后指令是

q
413 0 0 125 75 3086 cm
/I1 Do
Q

我认为它引用了资源I1

现在,在 XObjects 形式的情况下,iText 假定其内容引用的资源包含在其自己的资源字典中。

结果:iText 访问 null 字典并发生 NullPointerException

规范

PDF 规范 ISO 32000-1 规定:

A resource dictionary shall be associated with a content stream in one of the following ways:

  • For a content stream that is the value of a page’s Contents entry (or is an element of an array that is the value of that entry), the resource dictionary shall be designated by the page dictionary’s Resources or is inherited, as described under 7.7.3.4, "Inheritance of Page Attributes," from some ancestor node of the page object.

  • For other content streams, a conforming writer shall include a Resources entry in the stream's dictionary specifying the resource dictionary which contains all the resources used by that content stream. This shall apply to content streams that define form XObjects, patterns, Type 3 fonts, and annotation.

  • PDF files written obeying earlier versions of PDF may have omitted the Resources entry in all form XObjects and Type 3 fonts used on a page. All resources that are referenced from those forms and fonts shall be inherited from the resource dictionary of the page on which they are used. This construct is obsolete and should not be used by conforming writers.

(ISO 32000-1,第 7.8.3 节 - 资源字典)

因此,在本例中,我们处于过时选项三的情况,Q0 引用 Q0 所用页面的资源字典中定义的 XObject I1

相关文档有一个版本标题,声称符合 PDF 1.5(与 PDF 规范的 PDF 1.7 不同)。那么让我们看一下 PDF Reference 1.5。选项三对应的段落是:

  • A form XObject or a Type 3 font’s glyph description may omit the Resources entry, in which case resources will be looked up in the Resources entry of the page on which the form or font is used. This practice is not recommended.

总而言之,所讨论的 PDF 使用了 PDF 规范(2008 年发布,使用了九年!)称为过时的结构,甚至文件声称符合建议反对的 PDF 引用。另一方面,iText 不支持这种过时的构造。

想法如何解决这个问题

本质上,PDF Cleanup 代码必须扩展为

  • 记住PdfCleanUpProcessor中当前页面的资源和
  • PdfCleanUpContentOperator方法调用中使用这些当前页面资源,以防Do运算符引用表单XObject而没有自己的资源。

不幸的是,invoke 中使用的一些成员是私有(private)的。因此,人们必须复制 PdfCleanUp 代码或依靠反射。

(iText 5.5.12-快照)

iText 7

iText 7 PDF CleanUp 工具也会遇到 PDF 问题,这里的异常(exception)是 IllegalStateException,声称“图形状态在事件分派(dispatch)后始终被删除。如果您想将其保留在渲染器信息中,请在接收渲染器信息后使用preserveGraphicsState 方法。”

由于此异常是在事件调度期间引发的,因此此错误消息没有意义。不幸的是,PDF CleanUp 工具在 iText 7 中已成为闭源,因此查明问题并不那么容易。

(iText 7.0.3-快照;PDF CleanUp 1.0.2-快照)

关于java - 使用 itext 编辑 pdf 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43211367/

相关文章:

java - 从 Tomcat 5.0.27 升级到 Tomcat 7.0.30 后无法访问数据库

完全支持 CSS3 和 HTML5 的 Python HTML 到 PDF

php - PDF 可下载 PHP

javascript - 如何创建具有初始 View = Fit 的 PDF 文件

java - iText 数据矩阵生成问题

java - 如何使用 java XPATH 处理空 XML 标签?

Java:从字符串创建日期对象并插入MySQL

java - 无法使用 Mockito 抛出异常

python - Matplotlib 保存为 pdf 未在条形图中显示阴影线 - 潜在错误?

c# - 在 ITextSharp 免费许可证下动态创建 .pdf