Package org.apache.pdfbox.pdfparser
Class XrefTrailerResolver
- java.lang.Object
-
- org.apache.pdfbox.pdfparser.XrefTrailerResolver
-
public class XrefTrailerResolver extends java.lang.Object
This class will collect all XRef/trailer objects and creates correct xref/trailer information after all objects are read using startxref and 'Prev' information (unused XRef/trailer objects are discarded). In case of missing startxref or wrong startxref pointer all XRef/trailer objects are used to create xref table / trailer dictionary in order they occur. For each new xref object/XRef stream methodnextXrefObj(long, XRefType)
must be called with start byte position. All following calls tosetXRef(COSObjectKey, long)
orsetTrailer(COSDictionary)
will add the data for this byte position. After all objects are parsed the startxref position must be provided usingsetStartxref(long)
. This is used to build the chain of active xref/trailer objects used for creating document trailer and xref table.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
XrefTrailerResolver.XrefTrailerObj
A class which represents a xref/trailer object.static class
XrefTrailerResolver.XRefType
The XRefType of a trailer.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Long,XrefTrailerResolver.XrefTrailerObj>
bytePosToXrefMap
private XrefTrailerResolver.XrefTrailerObj
curXrefTrailerObj
private static org.apache.commons.logging.Log
LOG
Log instance.private XrefTrailerResolver.XrefTrailerObj
resolvedXrefTrailer
-
Constructor Summary
Constructors Constructor Description XrefTrailerResolver()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Set<java.lang.Long>
getContainedObjectNumbers(int objstmObjNr)
Returns object numbers which are referenced as contained in object stream with specified object number.COSDictionary
getCurrentTrailer()
Returns the trailer last set bysetTrailer(COSDictionary)
.COSDictionary
getFirstTrailer()
Returns the first trailer if at least one exists.COSDictionary
getLastTrailer()
Returns the last trailer if at least one exists.COSDictionary
getTrailer()
Gets the resolved trailer.int
getTrailerCount()
Returns the count of trailers.java.util.Map<COSObjectKey,java.lang.Long>
getXrefTable()
Gets the resolved xref table.XrefTrailerResolver.XRefType
getXrefType()
Returns the XRefTxpe of the resolved trailer.void
nextXrefObj(long startBytePos, XrefTrailerResolver.XRefType type)
Signals that a new XRef object (table or stream) starts.protected void
reset()
Reset all data so that it can be used to rebuild the trailer.void
setStartxref(long startxrefBytePosValue)
Sets the byte position of the first XRef (has to be called after very last startxref was read).void
setTrailer(COSDictionary trailer)
Adds trailer information for current XRef object.void
setXRef(COSObjectKey objKey, long offset)
Populate XRef HashMap of current XRef object.
-
-
-
Field Detail
-
bytePosToXrefMap
private final java.util.Map<java.lang.Long,XrefTrailerResolver.XrefTrailerObj> bytePosToXrefMap
-
curXrefTrailerObj
private XrefTrailerResolver.XrefTrailerObj curXrefTrailerObj
-
resolvedXrefTrailer
private XrefTrailerResolver.XrefTrailerObj resolvedXrefTrailer
-
LOG
private static final org.apache.commons.logging.Log LOG
Log instance.
-
-
Method Detail
-
getFirstTrailer
public final COSDictionary getFirstTrailer()
Returns the first trailer if at least one exists.- Returns:
- the first trailer or null
-
getLastTrailer
public final COSDictionary getLastTrailer()
Returns the last trailer if at least one exists.- Returns:
- the last trailer ir null
-
getTrailerCount
public final int getTrailerCount()
Returns the count of trailers.- Returns:
- the count of trailers.
-
nextXrefObj
public void nextXrefObj(long startBytePos, XrefTrailerResolver.XRefType type)
Signals that a new XRef object (table or stream) starts.- Parameters:
startBytePos
- the offset to start attype
- the type of the Xref object
-
getXrefType
public XrefTrailerResolver.XRefType getXrefType()
Returns the XRefTxpe of the resolved trailer.- Returns:
- the XRefType or null.
-
setXRef
public void setXRef(COSObjectKey objKey, long offset)
Populate XRef HashMap of current XRef object. Will add an Xreftable entry that maps ObjectKeys to byte offsets in the file.- Parameters:
objKey
- The objkey, with id and gen numbersoffset
- The byte offset in this file
-
setTrailer
public void setTrailer(COSDictionary trailer)
Adds trailer information for current XRef object.- Parameters:
trailer
- the current document trailer dictionary
-
getCurrentTrailer
public COSDictionary getCurrentTrailer()
Returns the trailer last set bysetTrailer(COSDictionary)
.- Returns:
- the current trailer.
-
setStartxref
public void setStartxref(long startxrefBytePosValue)
Sets the byte position of the first XRef (has to be called after very last startxref was read). This is used to resolve chain of active XRef/trailer. In case startxref position is not found we output a warning and use all XRef/trailer objects combined in byte position order. Thus for incomplete PDF documents with missing startxref one could call this method with parameter value -1.- Parameters:
startxrefBytePosValue
- starting position of the first XRef
-
getTrailer
public COSDictionary getTrailer()
Gets the resolved trailer. Might returnnull
in casesetStartxref(long)
was not called before.- Returns:
- the trailer if available
-
getXrefTable
public java.util.Map<COSObjectKey,java.lang.Long> getXrefTable()
Gets the resolved xref table. Might returnnull
in casesetStartxref(long)
was not called before.- Returns:
- the xrefTable if available
-
getContainedObjectNumbers
public java.util.Set<java.lang.Long> getContainedObjectNumbers(int objstmObjNr)
Returns object numbers which are referenced as contained in object stream with specified object number. This will scan resolved xref table for all entries having negated stream object number as value.- Parameters:
objstmObjNr
- object number of object stream for which contained object numbers should be returned- Returns:
- set of object numbers referenced for given object stream
or
null
ifsetStartxref(long)
was not called before so that no resolved xref table exists
-
reset
protected void reset()
Reset all data so that it can be used to rebuild the trailer.
-
-