Hello,
Occasionally, we receive the error below.  We ran the document through AI and 
found out that it is a valid document, even if it is not supported by most 
libraries.  It then suggested that when we experience this error, we should run 
it through a secondary loader designed to resolve the error.  That solution is 
now working for us.  However, we wanted to share this solution with the broader 
community in hopes of having it resolved within PDFBox itself.

Below is a synopsis of the issue, and the suggested solution.  It references a 
document, that it created, but I cannot attach it to this email.  I can forward 
it directly if needed.

If you have any questions or need any follow-up information, please don't 
hesitate to ask.

Thanks,
John Petersam
Interactive Consulting Services, Inc.



PDFBox bug report: object streams with non-delimited
members fail to parse
Observed on PDFBox 3.0.8 (Java 25/27, Windows).

Summary
Loader.loadPDF fails on documents whose object streams (/ObjStm) write their 
members
back-to-back with no whitespace separators. Per ISO 32000, object stream 
members are located by
the byte offsets declared in the stream's header - the spec does not require 
whitespace between
members - so these files appear to be spec-tolerable, but PDFBox cannot parse 
them. The failure
surfaces as the misleading "Page tree root must be a dictionary" error.

Symptom
java.io.IOException: Page tree root must be a dictionary
at org.apache.pdfbox.pdfparser.COSParser.checkPages(COSParser.java:1415)
at org.apache.pdfbox.pdfparser.PDFParser.initialParse(PDFParser.java:120)
at org.apache.pdfbox.Loader.loadPDF(Loader.java:235)
with the underlying cause logged by COSParser.parseObjectStreamObject:
java.io.IOException: Error expected floating point number
actual='76.32370.56'
at org.apache.pdfbox.cos.COSFloat.<init>(COSFloat.java:98)
at org.apache.pdfbox.pdfparser.BaseParser.parseCOSNumber(BaseParser.java:1075)
at org.apache.pdfbox.pdfparser.PDFObjectStreamParser.parseAllObjects
(PDFObjectStreamParser.java:157)
Caused by: java.lang.NumberFormatException: multiple points
Observed producer: PDFTron PDFNet V10.1008 (from the document's /Producer 
metadata).

Root cause
PDFObjectStreamParser seeks to each member's declared offset, then tokenizes 
forward until a
delimiter. When two adjacent members are numbers with no whitespace between 
them - e.g. 76.32
immediately followed by 370.56 - the tokenizer consumes 76.32370.56 as a single 
token, throws
NumberFormatException, and abandons the entire object stream. Since the parse 
is atomic, every
object in that stream (including the /Pages tree, if compressed) becomes 
unreachable; checkPages
then fails with "Page tree root must be a dictionary". The member's declared 
end offset is never
consulted during tokenization.
The same defect exists one level up: the object stream header (the 
object-number / offset pairs) is
parsed by tokenization rather than being bounded to the declared /First length. 
In an observed file
the last header pair (2416 5154) abuts the first member's bytes (397.2), 
merging into the token
5154397.2 - the parser only recovers because the resulting 
NumberFormatException happens to
terminate pair parsing.

Minimal reproducer
The attached malformed-packed-objstm.pdf (513 bytes, generated in-memory, no 
real-world
content) reproduces the failure on 3.0.8:
- Object stream 5 0: /N 3, /First 11, FlateDecode. Header "2 0 6 35 7 40" 
declares members at
offsets 0, 35, 40.
- Member data: "<</Type/Pages/Kids[3 0 R]/Count 1>>76.32370.56" - the /Pages 
dictionary
immediately followed by two adjacent floats packed with no separator.
- Xref is a compressed /XRef stream (type-2 entries for the compressed objects).
Loader.loadPDF(bytes) throws the IOException above. Splitting the packed 
members at their
declared offsets (35 and 40) yields two perfectly valid floats.

Suggested fix
- Bound each member's read to its declared byte range [first + offset, first + 
offsetEnd). Passing a
bounded source (or a stop offset) to the tokenizer would make a number cleanly 
terminate at the
member boundary instead of over-reading into the next member.
- Bound header parsing to /First bytes so a packed header is handled 
deterministically rather than
by relying on a tokenization error.
- Alternatively or additionally: on a tokenization failure inside a member, 
retry the member bounded
to its declared range rather than abandoning the whole object stream.

Workaround
Downstream, we recover these documents by parsing the xref chain and object 
streams at the byte
level and appending a standard PDF incremental update that re-emits each object 
stream member
as an ordinary uncompressed indirect object (new xref table + trailer /Prev). 
The original bytes are
untouched. We are happy to share this implementation if useful as a reference.

Note: if the PDFBox team considers these files malformed rather than 
spec-tolerable, the parse
failure could at minimum be scoped to the offending member rather than 
discarding the entire object
stream.

Reply via email to