0

Every once in a while I keep getting this error when combining PDF files. I am not sure what the error message means.

Rebuild failed: trailer not found.;

System.Exception: Encountered error while trying to combine PDF file [\xxxx\xxxx\9140\9140.3.46843.305403720000.10282014.pdf] to other PDF's. ---> iTextSharp.text.exceptions.InvalidPdfException: Rebuild failed: trailer not found.; Original message: PDF startxref not found. at iTextSharp.text.pdf.PdfReader..ctor(IRandomAccessSource byteSource, Boolean partialRead, Byte[] ownerPassword, X509Certificate certificate, ICipherParameters certificateKey, Boolean closeSourceOnConstructorError) at iTextSharp.text.pdf.PdfReader..ctor(String filename) at xxxx.SSUtilities.PDFUtility.ConcatenateFiles(String[] pathOfFilesToConcatenate, String pathOfFileToCombineInto)

The code:

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;
        }
JohnK
  • 35
  • 8
  • does it happen consistently with the same documents or is it an intermittent error? – Matt Coubrough Dec 11 '14 at 22:13
  • intermittent. Happens with several documents. – JohnK Dec 11 '14 at 22:19
  • 1
    We should know (1) the version of iText that is used, (2) the number of bytes at the end of the file before `%%EOF` occurs, and (3) the two lines preceding `%% EOF`. Old versions were less tolerant than new versions regarding trailing gibberish in the file. Of the second line before `%%EOF` isn't `xref`, your PDFs may suffer from a more serious problem. – Bruno Lowagie Dec 11 '14 at 23:16
  • I know we are using 5.5.3 per dll version. Request 2 & 3 will take a few days to get the details. – JohnK Dec 12 '14 at 16:21
  • 1
    Are you combining PDFs that _you_ previously made? I ask because there are two very common but incorrect patterns that we continuously see here that actually add garbage to the end of a PDF. The first is [the usage of a `MemoryStream` and calling `GetBuffer()` instead `ToArray()`](http://stackoverflow.com/a/5119739/231316). The second is more ASP.Net-based but there are non ASP.Net equivalents, and that is writing directly to the `Response.Output` stream and then calling `Response.Write()` on the `PdfDocument`. – Chris Haas Dec 12 '14 at 19:31
  • Yes. We are combining previously made pdf files. If you are correct in the common patterns you describe above, how do we go about correcting the problem? – JohnK Dec 15 '14 at 20:44
  • To add to my comment earlier about the frequency of the error, the problem we have only happens with maybe 3 documents out of 1000 processed each run. – JohnK Dec 16 '14 at 14:25
  • itextsharp's `PdfCopy` class is hopelessly broken. It's an exception generator. Use the deprecated `PdfCopyFields` class instead. It's still not fit for release, but it's usable if you're patient. – 15ee8f99-57ff-4f92-890c-b56153 Jul 23 '15 at 15:48

0 Answers0