Rebuild failed using PDF compression

末鹿安然 提交于 2019-12-11 07:49:00

问题


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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/26256195/rebuild-failed-using-pdf-compression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!