Search code examples
itext7

Append mode requires a document without errors, even if recovery is possible


The PDF which I signed with append mode is exported from Office Word 2016.

Here is my file : word.pdf

And I got this error message:

com.itextpdf.kernel.PdfException: Append mode requires a document without errors, even if recovery is possible.

I am using iText7 7.0.4 .


Solution

  • The document you are trying to change in append mode is broken. Most likely, the byte offsets as defined in the cross-reference table don't correspond with the actual byte positions of the PDF objects.

    In your case, I see something strange at the end of the file:

    xref
    0 26
    0000000010 65535 f
    0000000017 00000 n
    0000000166 00000 n
    0000000222 00000 n
    0000000492 00000 n
    0000000755 00000 n
    0000000932 00000 n
    0000001180 00000 n
    0000001233 00000 n
    0000001286 00000 n
    0000000011 65535 f
    0000000012 65535 f
    0000000013 65535 f
    0000000014 65535 f
    0000000015 65535 f
    0000000016 65535 f
    0000000017 65535 f
    0000000018 65535 f
    0000000019 65535 f
    0000000020 65535 f
    0000000000 65535 f
    0000001961 00000 n
    0000002154 00000 n
    0000044863 00000 n
    0000048000 00000 n
    0000048045 00000 n
    trailer
    <</Size 26/Root 1 0 R/Info 9 0 R/ID[<8812105F6F93284DAEF240C8C1FC4C4E><8812105F6F93284DAEF240C8C1FC4C4E>] >>
    startxref
    48341
    %%EOF
    xref
    0 0
    trailer
    <</Size 26/Root 1 0 R/Info 9 0 R/ID[<8812105F6F93284DAEF240C8C1FC4C4E><8812105F6F93284DAEF240C8C1FC4C4E>] /Prev 48341/XRefStm 48045>>
    startxref
    49017
    %%EOF
    

    You have a PDF with two trailers. One trailer claims that the cross-reference table in stored in a stream:

    /XRefStm 48045
    

    While at the same time indication the start of the cross-reference table at byte position 49017:

    startxref
    49017
    

    The other trailer claims that there's an uncompressed cross-reference table and that it starts at byte position 48341:

    startxref
    48341
    

    And indeed: there is an uncompressed cross-reference stream:

    xref
    0 26
    0000000010 65535 f
    0000000017 00000 n
    

    Do you understand the inconsistency in your file?

    When you use append mode, iText doesn't change anything to the original document: not a single byte is changed; new bytes are added after the final %%EOF marker of the original file. However, iText refuses to do this when the original file is broken. I hope you understand the rationale: you'd make a bad situation worse if iText allowed you to do this.

    To solve this problem, you need to fix the broken file first. That can be done by "manipulating" the document without changing anything, but to do this in normal mode, not in append mode.

    Have you tried removing the extra trailer. I threw away:

    xref
    0 0
    trailer
    <</Size 26/Root 1 0 R/Info 9 0 R/ID[<8812105F6F93284DAEF240C8C1FC4C4E><8812105F6F93284DAEF240C8C1FC4C4E>] /Prev 48341/XRefStm 48045>>
    startxref
    49017
    %%EOF
    

    Adobe Reader didn't complain after removing these bytes.