Skip to content

Commit d9bdbb6

Browse files
committed
Added a way to deal with incorrectly constructed XREF tables.
When an XREF table starts from any other number than 0, iText would add the 0000 65535 entry twice. A fix has been added that circumvents this bizarre entry. SUP-1557
1 parent 47ff387 commit d9bdbb6

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfReader.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ This file is part of the iText (R) project.
5454
import com.itextpdf.kernel.PdfException;
5555
import com.itextpdf.kernel.pdf.filters.FilterHandlers;
5656
import com.itextpdf.kernel.pdf.filters.IFilterHandler;
57-
import org.slf4j.Logger;
58-
import org.slf4j.LoggerFactory;
5957

6058
import java.io.ByteArrayInputStream;
6159
import java.io.Closeable;
@@ -65,6 +63,9 @@ This file is part of the iText (R) project.
6563
import java.text.MessageFormat;
6664
import java.util.Map;
6765

66+
import org.slf4j.Logger;
67+
import org.slf4j.LoggerFactory;
68+
6869
public class PdfReader implements Closeable, Serializable {
6970

7071
private static final long serialVersionUID = -3584187443691964939L;
@@ -750,6 +751,22 @@ protected PdfDictionary readXrefSection() throws IOException {
750751
tokens.nextValidToken();
751752
int gen = tokens.getIntValue();
752753
tokens.nextValidToken();
754+
if ( pos == 0L && gen == 65535 && num == 1 ) {
755+
// Very rarely can an XREF have an incorrect start number. (SUP-1557)
756+
// e.g.
757+
// xref
758+
// 1 13
759+
// 0000000000 65535 f
760+
// 0000000009 00000 n
761+
// 0000215136 00000 n
762+
// [...]
763+
// Because of how iText reads (and initializes) the XREF, this will lead to the XREF having two 0000 65535 entries.
764+
// This throws off the parsing and other operations you'd like to perform.
765+
// To fix this we reset our index and decrease the limit when we've encountered the magic entry at position 1.
766+
num = 0;
767+
end--;
768+
continue;
769+
}
753770
PdfIndirectReference reference = xref.get(num);
754771
if (reference == null) {
755772
reference = new PdfIndirectReference(pdfDocument, num, gen, pos);

0 commit comments

Comments
 (0)