iText-PdfReader: Rebuild failed: Dictionary key en

2019-05-29 07:30发布

问题:

I am using version 5.4.4 and I have a problem with the com.itextpdf.text.pdf.PdfReader.

When I read a PDF with

PdfReader reader = new PdfReader(„Test.pdf");
  • its’s a scanned sheet and I can open it with Adobe Reader - following error occures:

com.lowagie.text.exceptions.InvalidPdfException: Rebuild failed: Dictionary key endstream is not a name. at file pointer 3913220; Original message: Dictionary key endstream is not a name. at file pointer 3913220
at com.lowagie.text.pdf.PdfReader.readPdf(PdfReader.java:668)
at com.lowagie.text.pdf.PdfReader.(PdfReader.java:189)
at com.lowagie.text.pdf.PdfReader.(PdfReader.java:264)
at com.lowagie.text.pdf.PdfReader.(PdfReader.java:247)

I look in the code and since itext 5.4.4 an exception is thrown. In older versions, this exception is ignored.

Code from method readDocObj() in itext 5.4.3 on line 1319:

catch (Exception e) {  
    obj = null;         // Exception ignored !!!!!!!!  
}

Code from method readDocObj() in itext 5.4.4 on line 1319:

catch (IOException e) {  
    if (debugmode) {
        e.printStackTrace();  
        obj = null;  
    }  
    else  
        throw e;        // Exception thrown !!!!!!  
}

In the newest itext-Version 5.5.1 on line 1346:

    catch (IOException e) {  
        if (debugmode) {  
            if (LOGGER.isLogging(Level.ERROR))  
                LOGGER.error(e.getMessage(), e);  
            obj = null;  
        }  
        else  
            throw e;        // Exception thrown !!!!!!  
    }  

回答1:

Your original PDF contains an error and ignoring that error can lead to very strange results (missing objects, completely screwed-up PDFs). Therefore, we should throw an error, informing you what is wrong with the file. Your PDF probably contains a PDF stream of which the stream dictionary is not correct.

Arguing that the file opens well in Adobe Reader is a poor argument: it doesn't mean that your PDF obeys ISO-32000-1. Adobe Reader does a best effort to open even files that are broken. If you would open it with Adobe Acrobat and use Preflight, you'll notice that Acrobat complains about that error too.

As it was very hard to debug files with such an error, we introduced the debugmode constant. This constant is false by default, but set to true in iText RUPS. Setting this constant to true allows us to read the file and ignore the errors so that we can examine the file in RUPS.

If you don't care about the quality of your input PDFs, you can set debugmode to true, but I wouldn't recommend doing that. I would recommend fixing the original PDF.

Remark: I upvoted the comment. You are clearly not telling us the full truth as you are claiming that you are using an iText 5 version, yet you have error messages mentioning my name (I am the Lowagie from the com.lowagie packages).



标签: java itext