简体   繁体   English

Itextsharp在标记某些文本后创建损坏的pdf

[英]Itextsharp creates a corrupted pdf after stamping some text

Just stamping some text into a pdf and itextsharp creats a corrupted file. 仅将一些文本标记为pdf,itextsharp就会创建损坏的文件。 When tried to read the pdf it throws error as follows 尝试阅读pdf时会引发如下错误

An exception of type 'iTextSharp.text.exceptions.InvalidPdfException' Additional information: The document has no page root (meaning: it's an invalid PDF). 类型为'iTextSharp.text.exceptions.InvalidPdfException'的异常其他信息:该文档没有页面根目录(意味着:这是无效的PDF)。

Following code is used to edit the pdf and stamp text content 以下代码用于编辑pdf和图章文本内容

using (PdfReader pdfReader = new PdfReader(System.IO.File.ReadAllBytes(pdfPath)))
           using (Stream pdfStream = new FileStream(pdfPath, FileMode.Open, FileAccess.ReadWrite))
           {
               PdfReaderContentParser parserReason = new PdfReaderContentParser(pdfReader);
               PdfStamper pdfStamper = new PdfStamper(pdfReader, pdfStream);
               PdfContentByte pdfContentByte = pdfStamper.GetOverContent(pdfReader.NumberOfPages);
               BaseFont baseFont = BaseFont.CreateFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
               pdfContentByte.SetColorFill(BaseColor.BLACK);
               pdfContentByte.SetFontAndSize(baseFont, 12);
               pdfContentByte.BeginText();
               TextMarginFinder finderReason = parserReason.ProcessContent(pdfReader.NumberOfPages, new iTextSharp.text.pdf.parser.TextMarginFinder());
               pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Some text  : " + annotation, finderReason.GetLlx(), finderReason.GetLly() - 20f, 0);
               pdfContentByte.EndText();
               pdfStamper.Close();

           }

The pdf files are created with apache fop 1.1 and itextsharp is used to edit the file.The issue is not happening with all pdf but only with some files. pdf文件是使用apache fop 1.1创建的,而itextsharp用于编辑文件。不是所有的pdf都存在此问题,而是仅某些文件发生了此问题。 You can find the PDF which creates the issue here 您可以在此处找到造成问题的PDF

The issue is that you are opening the file stream like this: 问题是您正在打开文件流,如下所示:

using (Stream pdfStream = new FileStream(pdfPath, FileMode.Open, FileAccess.ReadWrite))

FileMode.Open leaves the old content in place, writing to it merely overwrites it. FileMode.Open将旧内容FileMode.Open在原位,对其进行写入只是将其覆盖。 In particular, if the new document is shorter than the original one, an old tail piece of the original document remains. 特别是,如果新文档比原始文档短,则保留原始文档的旧尾部。 As the PDF cross references are at its end, this results in the old cross references being applied to the new document. 由于PDF交叉引用位于末尾,因此将旧的交叉引用应用于新文档。 This obviously does not match. 这显然不匹配。

If your use FileMode.Create instead, this issue does not happen. 如果改为使用FileMode.Create ,则不会发生此问题。


By the way, your code completely fails for the sample file you provided because that sample file has no text on the final page. 顺便说一句,您提供的示例文件的代码完全失败,因为该示例文件在最后一页上没有文本。 Thus, finderReason determines no margins rectangle, your access to finderReason.GetLlx() tries to access a null rectangle member, and consequentially it fails. 因此, finderReason确定没有边距矩形,您对finderReason.GetLlx()访问尝试访问一个null矩形成员,因此它失败了。 You should add some appropriate checks. 您应该添加一些适当的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM