简体   繁体   English

ITextSharp合并Pdf异常

[英]ITextSharp Merge Pdf Exception

I'm trying to merge a whole bunch of pdfs together. 我正在尝试将一大堆pdf合并在一起。 This is what I have so far 这就是我到目前为止

            using (FileStream stream = new FileStream(OutputDirectory+"/"+ OutputFileName+".pdf", FileMode.Create))
            {
                Document pdfDoc = new Document();
                PdfCopy pdf = new PdfCopy(pdfDoc, stream);
                pdfDoc.Open();
                Console.WriteLine("Merging files count: " + Input.Count);
                int i = 1;
                foreach (var item in Input)
                {
                    Console.WriteLine(i + ". Adding: " + item.FileName);
                    pdf.AddDocument(new PdfReader(item.FileWithPath));
                    i++;
                }
                pdfDoc.Close();
                Console.WriteLine("PDF merge complete.");
            }

However, after some pdfs have been merged properly, I get this exception: 但是,在正确合并了一些pdf之后,我得到了这个异常:

iTextSharp.text.exceptions.InvalidPdfException: 'Rebuild failed: trailer not found.; iTextSharp.text.exceptions.InvalidPdfException:'重建失败:找不到预告片。 Original message: PDF startxref not found.' 原始消息:未找到PDF startxref。

Its pointing at my pdf.AddDocument(new PdfReader line 它指向我的pdf.AddDocument(新的PdfReader行

I have looked around and some comments have mentioned that my file is corrupt. 我环顾四周,有些评论提到我的文件已损坏。 However, I am able to open and read the source pdfs without any issue. 但是,我可以打开并阅读pdf文件,没有任何问题。 I am unsure of how to continue now. 我不确定现在如何继续。

I have looked around and some comments have mentioned that my file is corrupt. 我环顾四周,有些评论提到我的文件已损坏。

The information you found most likely is correct, the file you try to read is likely to be corrupt. 您找到的信息最有可能是正确的,您尝试读取的文件可能已损坏。

However, I am able to open and read the source pdfs without any issue. 但是,我可以打开并阅读pdf文件,没有任何问题。

PDF viewers often try to repair a certain amount of corruption under the hood. PDF查看器经常尝试在引擎盖下修复一定数量的损坏。 As the person viewing the PDF can usually quickly recognize whether the repair succeeded or only left some pages full of garbage, this is ok-ish, ie less a bug and more a feature. 由于查看PDF的人员通常可以快速识别出修复是成功还是仅保留了几页垃圾,因此这是可以的,即少了一个错误,而是一个功能。

Libraries that automatically process PDFs, on the other hand, should not try this (at least not as much as viewers do) as their outputs might directly go into some archive never to be checked until an audit some years later. 另一方面,自动处理PDF的库不应尝试这样做(至少不像查看者那样尝试),因为它们的输出可能会直接进入某个存档,直到几年后才进行审核。 A document full of garbage then will cause lots of trouble. 充满垃圾的文档将造成很多麻烦。

I am unsure of how to continue now. 我不确定现在如何继续。

Try to repair the PDF in question. 尝试修复有问题的PDF。

If you open it in a current Adobe Acrobat Reader, the program usually upon closing the document will ask whether you want to save the document. 如果您在当前的Adobe Acrobat Reader中打开它,则该程序通常在关闭文档时会询问您是否要保存该文档。 This will actually save a repaired version which iText is very likely to accept without further ado. 实际上,这将保存一个修复的版本,iText很可能会接受,而无需多加修改。

If that does not work, ie if either Adobe Acrobat Reader does not offer to safe a repaired version or iText does not even accept the repaired versions, please share the PDF in question here for further analysis. 如果这不起作用,即Adobe Acrobat Reader无法提供安全的修复版本或iText甚至不接受修复的版本,请在此处共享有问题的PDF进行进一步分析。

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

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