java - 删除 PDF 中的 PDFID

标签 java pdf itext

我正在使用 iText 将 xhtml 转换为 pdf。之后,我正在构建生成的 pdf 的 md5 校验和,以仅存储新的/更改的文件。

每个创建的文件都包含一个 PdfID0 和 PdfID1,它们看起来像散列。

那些“哈希”是做什么用的?以及如何删除它们?

我正在使用 iText 包中的以下代码来更改元信息:

        com.lowagie.text.pdf.PdfReader reader = new PdfReader(pdfPath);

        com.lowagie.text.pdf.PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(tempFile));
        HashMap<String, String> hMap = reader.getInfo();          
        hMap.put("Title", "MyTitle");
        hMap.put("Subject", "Subject");
        hMap.put("Keywords", "Key, words, here");
        hMap.put("Creator", "me");
        hMap.put("Author", "me");
        hMap.put("Producer", "me");
        hMap.put("CreationDate", null);
        hMap.put("ModDate", null);
        hMap.put("DocChecksum", null);

        stamper.setMoreInfo(hMap);
        stamper.close(); 

以及使用 pdftk 提取的文件元数据:

InfoKey: Creator
InfoValue: me
InfoKey: Title
InfoValue: MyTitle
InfoKey: Author
InfoValue: me
InfoKey: Producer
InfoValue: me
InfoKey: Keywords
InfoValue: Key, words, here
InfoKey: Subject
InfoValue: Subject
PdfID0: 28c71a8d7790a4d3e85ce879a90dec0
PdfID1: 4c5865d36c7a381e6166d5e362d0aafc
NumberOfPages: 1

感谢任何提示

最佳答案

您看到的标记为 PdfID0PdfID1通过 pdftk的元数据转储是以下 PDF 的一部分 trailer相应 PDF 文件末尾的代码(示例):

trailer
   << /Size 32
      /Root 24 R
      /Info 19 R
      /ID [ 
            <28c71a8d7790a4d3e85ce879a90dec0>
            <4c5865d36c7a381e6166d5e362d0aafc>
          ]
   >> startxref
81799
%%EOF

/ID仅当 Encrypt 时才需要预告片字典中的条目条目存在;否则它是一个可选的 key 。

PDF 规范将其描述为:

"An array of two byte-strings constituting a file identifier (see 14.4, "File Identifiers") for the file. If there is an Encrypt entry this array and the two byte-strings shall be direct objects and shall be unencrypted."

此外:

"The first byte string shall be a permanent identifier based on the contents of the file at the time it was originally created and shall not change when the file is incrementally updated. The second byte string shall be a changing identifier based on the file’s contents at the time it was last updated. When a file is first written, both identifiers shall be set to the same value. If both identifiers match when a file reference is resolved, it is very likely that the correct and unchanged file has been found. If only the first identifier matches, a different version of the correct file has been found."

而且它不是 necesarrily 散列。以下是 ISO PDF 规范建议(而非“规定”):

"To help ensure the uniqueness of file identifiers, they should be computed by means of a message digest algorithm such as MD5 (described in Internet RFC 1321, The MD5 Message-Digest Algorithm; see the Bibliography), using the following information:

  • The current time
  • A string representation of the file’s location, usually a pathname
  • The size of the file in bytes
  • The values of all entries in the file’s document information dictionary (see 14.3.3, “Document Information Dictionary”)

生成的 PDF 文件中还有一些位置可能会随着每次新运行而改变。文档信息字典中的这些键(预告片中引用的 /Info 条目)

  • /CreationDate
  • /ModDate

可能会在您每次创建或修改 PDF 时更新。

因此,在生成的 PDF 上使用您自己的 MD5 校验和来检查新的/更改的文件将不起作用,除非您确保至少“规范化”/CreationDate/ModDate以及 /ID创建 MD5 哈希之前的条目。


更新:正如用户 mkl 在对该答案的评论中正确指出的那样,/CreationDate/ModDate /Info 的键字典(以及 /ID 信息)通常在 PDF 中嵌入的 XML 元数据中包含等效的信息片段。您可以在 pdfinfo 的帮助下显示完整的 XML 元数据。像这样的实用程序:

pdfinfo -meta your.pdf

关于java - 删除 PDF 中的 PDFID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13193820/

相关文章:

android - 通过intent/Fileprovider共享文档

java - 无法在itext表中动态显示mysql数据库中的blob图像

java - 通过 Java 批处理 : iText or Apache FOP? 简单生成 PDF

java - 如何将二维数组转换为一维数组?

java - 使用 NotesFactory 创建 session

pdf - 在 Angular 2 中生成 pdf

java - 如何合并包含 PDF 和图像的文档?

java - 使用 XMPP、Spark 或 SparkWeb java 的 Facebook 聊天应用程序

java - Spring Boot 2 嵌入式Servlet容器的编程配置

html - 将 HTML 页面转换为 PDF 时修复 PDF 辅助功能问题(注释缺少替代描述)