iText 7.0.4.0 - PdfWriter 为某些 PDF 文件输入生成损坏的 PDF
iText 7.0.4.0 - PdfWriter produces corrupted PDF for certain PDF file inputs
我遇到一个问题,iText 7.0.4.0 (.NET 4.5.1) 中的 PdfWriter 为某些输入的 PDF 文件生成损坏的 PDF 文档。
具体来说,段落格式正确的 PDF 文件没有问题。但是,如果输入的 PDF 包含不规则的内容(缺少更好的词;请参考 Google drive 中的示例),PdfWriter 会生成损坏的 PDF 文件;损坏,我的意思是文件可以打开,但它显示一个空白页面,缩放比例极高(在 Adobe Reader XI 中)。在上述 Google 驱动器 link.
中也提供了损坏的示例
示例代码:
using (var pdfReader = new PdfReader("sample1_input.pdf"))
{
PdfDocument pdfDoc = new PdfDocument(pdfReader, new PdfWriter("sample1_corrupted_output.pdf"));
// Trying to highlight a part of PDF by referencing this example:
// https://developers.itextpdf.com/examples/stamping-content-existing-pdfs/clone-highlighting-text
// Commented out for now because PdfWriter is producing corrupted PDF documents for the samples and similar PDF files.
//PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage());
//canvas.SetExtGState(new PdfExtGState().SetFillOpacity(0.1f));
//canvas.SaveState();
//canvas.SetFillColor(Color.YELLOW);
//canvas.Rectangle(100, 100, 200, 200);
//canvas.Fill();
//canvas.RestoreState();
pdfDoc.Close(); // Corrupted PDF file is produced, even without highlighting.
}
我注意到的一件事 "interesting" 是,如果我提供 "new StampingProperties().UseAppendMode()" 作为 PdfDocument 的第三个参数(没有突出显示代码),PdfWriter 会吐出原始文件(虽然比大几 kb)出于某种原因的原始版本)。但是,当突出显示代码未被注释时,PdfWriter 会返回生成损坏的 PDF。
Link 示例文件:
https://drive.google.com/open?id=0B3NPOZswWocQV09KMW5fbFVyUm8
sample1_input.pdf(输入样本 #1)-> sample1_corrupted_output.pdf(损坏的输出)
sample2_input.pdf(输入样本 #2)-> sample2_corrupted_output.pdf(损坏的输出)
请多多指教
导致此损坏的原因是相关 PDF 的页面树结构异常:
这在两个方面是不寻常的:
- 它有一个没有任何页面对象的子树(字典 17 和 21)。
- 它有一个混合子节点类型的节点(字典 10 有一个 Page 子节点 3 和一个 Pages 子节点 17)
如果删除无页子树(通过从对象 10 的 Kids 中删除对象 17),两个怪癖都会被删除并且代码不会再失败。
虽然这两个怪癖都很奇怪,但我在 ISO 32000-1 中没有看到任何内容(不幸的是我还没有 ISO 32000-2 的副本)表明这些不寻常的结构是明确禁止的。因此,我认为这是一个 iText 错误。
我可以为 Java 重现 iText 7.0.4 的问题,但不能重现 7.0.5 的当前开发快照。
确实,有一个日期为 2017-09-19 10:03:37 [c0b35f0] 的提交被描述为 "Fix bugs in pages tree rebuilding",代码块中的 PdfPagesTree
class 有所不同描述为 "handle mix of PdfPage and PdfPages"。因此,这个问题似乎是已知的并且已经得到解决。
您可以等待 7.0.5 发布或寻找修补程序 7.0.4.x.
我遇到一个问题,iText 7.0.4.0 (.NET 4.5.1) 中的 PdfWriter 为某些输入的 PDF 文件生成损坏的 PDF 文档。
具体来说,段落格式正确的 PDF 文件没有问题。但是,如果输入的 PDF 包含不规则的内容(缺少更好的词;请参考 Google drive 中的示例),PdfWriter 会生成损坏的 PDF 文件;损坏,我的意思是文件可以打开,但它显示一个空白页面,缩放比例极高(在 Adobe Reader XI 中)。在上述 Google 驱动器 link.
中也提供了损坏的示例示例代码:
using (var pdfReader = new PdfReader("sample1_input.pdf"))
{
PdfDocument pdfDoc = new PdfDocument(pdfReader, new PdfWriter("sample1_corrupted_output.pdf"));
// Trying to highlight a part of PDF by referencing this example:
// https://developers.itextpdf.com/examples/stamping-content-existing-pdfs/clone-highlighting-text
// Commented out for now because PdfWriter is producing corrupted PDF documents for the samples and similar PDF files.
//PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage());
//canvas.SetExtGState(new PdfExtGState().SetFillOpacity(0.1f));
//canvas.SaveState();
//canvas.SetFillColor(Color.YELLOW);
//canvas.Rectangle(100, 100, 200, 200);
//canvas.Fill();
//canvas.RestoreState();
pdfDoc.Close(); // Corrupted PDF file is produced, even without highlighting.
}
我注意到的一件事 "interesting" 是,如果我提供 "new StampingProperties().UseAppendMode()" 作为 PdfDocument 的第三个参数(没有突出显示代码),PdfWriter 会吐出原始文件(虽然比大几 kb)出于某种原因的原始版本)。但是,当突出显示代码未被注释时,PdfWriter 会返回生成损坏的 PDF。
Link 示例文件: https://drive.google.com/open?id=0B3NPOZswWocQV09KMW5fbFVyUm8 sample1_input.pdf(输入样本 #1)-> sample1_corrupted_output.pdf(损坏的输出) sample2_input.pdf(输入样本 #2)-> sample2_corrupted_output.pdf(损坏的输出)
请多多指教
导致此损坏的原因是相关 PDF 的页面树结构异常:
这在两个方面是不寻常的:
- 它有一个没有任何页面对象的子树(字典 17 和 21)。
- 它有一个混合子节点类型的节点(字典 10 有一个 Page 子节点 3 和一个 Pages 子节点 17)
如果删除无页子树(通过从对象 10 的 Kids 中删除对象 17),两个怪癖都会被删除并且代码不会再失败。
虽然这两个怪癖都很奇怪,但我在 ISO 32000-1 中没有看到任何内容(不幸的是我还没有 ISO 32000-2 的副本)表明这些不寻常的结构是明确禁止的。因此,我认为这是一个 iText 错误。
我可以为 Java 重现 iText 7.0.4 的问题,但不能重现 7.0.5 的当前开发快照。
确实,有一个日期为 2017-09-19 10:03:37 [c0b35f0] 的提交被描述为 "Fix bugs in pages tree rebuilding",代码块中的 PdfPagesTree
class 有所不同描述为 "handle mix of PdfPage and PdfPages"。因此,这个问题似乎是已知的并且已经得到解决。
您可以等待 7.0.5 发布或寻找修补程序 7.0.4.x.