ItextSharp结合PDF错误

时间:2014-12-11 22:09:30

标签: pdf itextsharp

我偶尔会在组合PDF文件时遇到此错误。我不确定错误消息的含义。

重建失败:未找到预告片。;

System.Exception:尝试将PDF文件[\ xxxx \ xxxx \ 9140 \ 9140.3.46843.305403720000.10282014.pdf]合并到其他PDF时遇到错误。 ---> iTextSharp.text.exceptions.InvalidPdfException:重建失败:未找到预告片。原始消息:未找到PDF startxref。    在iTextSharp.text.pdf.PdfReader..ctor(IRandomAccessSource byteSource,Boolean partialRead,Byte [] ownerPassword,X509Certificate certificate,ICipherParameters certificateKey,Boolean closeSourceOnConstructorError)    在iTextSharp.text.pdf.PdfReader..ctor(String filename)    at xxxx.SSUtilities.PDFUtility.ConcatenateFiles(String [] pathOfFilesToConcatenate,String pathOfFileToCombineInto)

代码:

string currentFile = string.Empty;

        try
        {
            int pageOffset = 0;
            int f = 0;

            Document document = null;
            PdfCopy writer = null;

            while (f < pathOfFilesToConcatenate.Length)
            {
                currentFile = pathOfFilesToConcatenate[f];

                // we create a reader for a certain document
                PdfReader reader = new PdfReader(pathOfFilesToConcatenate[f]);
                reader.ConsolidateNamedDestinations();
                // we retrieve the total number of pages
                int n = reader.NumberOfPages;
                pageOffset += n;
                if (f == 0)
                {
                    // step 1: creation of a document-object
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    // step 2: we create a writer that listens to the document
                    writer = new PdfCopy(document, new FileStream(pathOfFileToCombineInto, FileMode.Create));
                    // step 3: we open the document
                    document.Open();
                }
                // step 4: we add content

                for (int i = 0; i < n; )
                {
                    ++i;
                    if (writer != null)
                    {
                        PdfImportedPage page = writer.GetImportedPage(reader, i);
                        writer.AddPage(page);
                    }
                }

                PRAcroForm form = reader.AcroForm;
                if (form != null && writer != null)
                {
                    //writer.CopyAcroForm(reader);
                    writer.Close();
                }
                f++;
            }
            // step 5: we close the document
            if (document != null)
            {
                document.Close();
            }
        }
        catch (Exception ex)
        {
            _logger.Error(new Exception("Encountered error while trying to combine PDF file [" + currentFile + "] to other PDF's.", ex));
            throw ex;
        }

0 个答案:

没有答案