1

Im trying to use the methods described bt kuujinbo here. PDF Compression with iTextSharp

This is my code, and it results in this error: "Rebuild failed: trailer not found.; Original message: PDF startxref not found."

            PdfReader reader = new PdfReader(output.ToArray());
            ReduceResolution(reader, 9);

            // Save altered PDF. then you can pass the btye array to a database, etc
            using (MemoryStream ms = new MemoryStream())
            {
                using (PdfStamper stamper = new PdfStamper(reader, ms))
                {
                }

                document.Close();
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename=Produktark-{0}.pdf", myItem.Key));
                Response.BinaryWrite(output.ToArray());
            }

What might I be missing?

Community
  • 1
  • 1
Martin at Mennt
  • 5,677
  • 13
  • 61
  • 89
  • What is in the *output* variable in your code? The message seems to indicate that it is not a PDF, at least not a complete one. – mkl Jan 09 '15 at 05:40

2 Answers2

3

An exception stating Rebuild failed: ...; Original message: ... is thrown by iText only during PdfReader initialization, i.e. in your case in the line

PdfReader reader = new PdfReader(output.ToArray());

and it indicates that the read data, i.e. output.ToArray(), does not constitute a valid PDF. You should write output.ToArray() to some file, too, and inspect it.

If you wonder why the message indicates that some Rebuild failed... you actually don't get the initial error but a follow-up one, the PDF digesting code has multiple blocks like this

try {
    read some part of the PDF;
} catch(Exception) {
    try {
        try to repair that part of the PDF and read it;
    } catch(Exception) {
        throw "Rebuild failed: ...; Original message: ...";
    }
}

In your case the part of interest was the cross reference table/stream and the issue was that the PDF startxref (a statement containing the offset of the cross reference start in the document) was not found.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thanks for your comments. If I do like this: ´Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", string.Format("attachment;filename=Produktark-{0}.pdf", myItem.Key)); Response.BinaryWrite(output.ToArray());´ it works, the PDF is rendered normally. So something happens when I try to use the other code. – Martin at Mennt Oct 08 '14 at 12:52
  • Just to be clear (and as @mkl said), your exception is being thrown on the first line of code you posted, correct? If that's the case, the code _after_ it isn't relevant, its the code that's _before_ it that we need. – Chris Haas Oct 08 '14 at 13:21
  • *So something happens when I try to use the other code.* - The input you give to the reader is broken. That something is displayed when you return it to a PDF renderer in some browser does hardly prove anything as these renderers often are very forgiving. Thus: **You should write output.ToArray() to some file, too, and inspect it.** – mkl Oct 08 '14 at 13:27
2

When I receive this error message it is caused by not closing the PDFStamper that I am using to edit the form fields.

Stamper.Close();

Must call before closing the PDF or will throw specified error.

Luddit3
  • 21
  • 1