Top Description Fields Constructors Methods
org.apache.pdfbox.cos

public Class COSDocument

extends COSBase
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.Closeable, .IOException, java.util.ArrayList, .HashMap, .List, .Map, .Map.Entry, java.util.stream.Collectors, org.apache.commons.logging.Log, .LogFactory, org.apache.pdfbox.io.IOUtils, .RandomAccessStreamCache, .RandomAccessStreamCache.StreamCacheCreateFunction

This is the in-memory representation of the PDF document. You need to call close() on this object when you are done using it!!
Author
Ben Litchfield

Field Summary

Modifier and TypeField and Description
private boolean
private final COSDocumentState
private boolean
private long
highestXRefObjectNumber

Used for incremental saving, to avoid XRef object numbers from being reused.

private boolean
isDecrypted

Signal that document is already decrypted.

private boolean
private static final Log
LOG

Log instance.

private final Map<COSObjectKey, COSObject>
objectPool

Maps ObjectKeys to a COSObject.

private final ICOSParser
private long
private final RandomAccessStreamCache
private final List<COSStream>
streams

List containing all streams which are created when creating a new pdf.

private COSDictionary
trailer

Document trailer dictionary.

private float
private final Map<COSObjectKey, Long>
xrefTable

Maps object and generation id to object byte offsets.

Constructor Summary

AccessConstructor and Description
public
COSDocument()

Constructor.

public
COSDocument(ICOSParser
Parser to be used to parse the document on demand
parser
)

Constructor.

public
COSDocument(RandomAccessStreamCache.StreamCacheCreateFunction
a function to create an instance of a stream cache
streamCacheCreateFunction
)

Constructor that will use the provided function to create a stream cache for the storage of the PDF streams.

public
COSDocument(RandomAccessStreamCache.StreamCacheCreateFunction
a function to create an instance of a stream cache
streamCacheCreateFunction
,
ICOSParser
Parser to be used to parse the document on demand
parser
)

Constructor that will use the provided function to create a stream cache for the storage of the PDF streams.

Method Summary

Modifier and TypeMethod and Description
public void
accept(ICOSVisitor
The object to notify when visiting this object.
visitor
)

Implements abstract org.apache.pdfbox.cos.COSBase.accept.

visitor pattern double dispatch method.

public void
addXRefTable(Map<COSObjectKey, Long>
xref table entries to be added
xrefTableValues
)

Populate XRef HashMap with given values.

public void
close()

Implements java.io.Closeable.close.

This will close all storage and delete the tmp files.

public COSStream

Returns:

the new COSStream
createCOSStream
()

Creates a new COSStream using the current configuration for scratch files.

public COSStream

Returns:

the new COSStream
createCOSStream
(COSDictionary
the corresponding dictionary
dictionary
,
long
the start position within the source
startPosition
,
long
the stream length
streamLength
)

Creates a new COSStream using the current configuration for scratch files.

public COSArray

Returns:

The document id.
getDocumentID
()

This will get the document ID.

public COSDocumentState

Returns:

The COSDocumentState of this COSDocument.
getDocumentState
()

Returns the COSDocumentState of this COSDocument.

public COSDictionary

Returns:

The encryption dictionary.
getEncryptionDictionary
()

This will get the encryption dictionary if the document is encrypted or null if the document is not encrypted.

public long

Returns:

The object number of the highest XRef stream, or 0 if there was no XRef stream.
getHighestXRefObjectNumber
()

Internal PDFBox use only.

public COSDictionary

Returns:

the dictionary containing the linearization information
getLinearizedDictionary
()

Get the dictionary containing the linearization information if the pdf is linearized.

public COSObject

Returns:

The object in the pool or a new one if it has not been parsed yet.
getObjectFromPool
(COSObjectKey
The object key.
key
)

This will get an object from the pool.

public List<COSObject>

Returns:

This will return all objects with the specified type.
getObjectsByType
(COSName
The type of the object.
type
)

This will get all dictionaries objects by type.

public List<COSObject>

Returns:

This will return all objects with the specified type(s).
getObjectsByType
(COSName
The first possible type of the object, mandatory.
type1
,
COSName
The second possible type of the object, usually an abbreviation, optional.
type2
)

This will get all dictionaries objects by type.

private List<COSObject>
public long

Returns:

a long with the old position of the startxref
getStartXref
()

Return the startXref Position of the parsed document.

private RandomAccessStreamCache
public COSDictionary

Returns:

the document trailer dict
getTrailer
()

This will get the document trailer.

public float

Returns:

The header version.
getVersion
()

This will get the version extracted from the header of this PDF document.

public Map<COSObjectKey, Long>

Returns:

mapping of ObjectsKeys to byte offsets
getXrefTable
()

Returns the xrefTable which is a mapping of ObjectKeys to byte offsets in the file.

public boolean

Returns:

true if the pdf has hybrid cross references
hasHybridXRef
()

Determines if the pdf has hybrid cross references, both plain tables and streams.

public boolean

Returns:

true if the document is already closed, false otherwise
isClosed
()

Returns true if this document has been closed.

public boolean

Returns:

true indicates that the pdf is decrypted.
isDecrypted
()

Indicates if a encrypted pdf is already decrypted after parsing.

public boolean

Returns:

true If this document is encrypted.
isEncrypted
()

This will tell if this is an encrypted document.

public boolean

Returns:

true if the trailer is a XRef stream
isXRefStream
()

Determines if the trailer is a XRef stream or not.

public void
setDecrypted()

Signals that the document is decrypted completely.

public void
setDocumentID(COSArray
The document id.
id
)

This will set the document ID. This should be an array of two strings.

public void
setEncryptionDictionary(COSDictionary
The encryption dictionary.
encDictionary
)

This will set the encryption dictionary, this should only be called when encrypting the document.

public void
setHasHybridXRef()

Marks the pdf as document using hybrid cross references.

public void
setHighestXRefObjectNumber(long
The object number of the highest XRef stream.
highestXRefObjectNumber
)

Internal PDFBox use only.

public void
setIsXRefStream(boolean
the new value for isXRefStream
isXRefStreamValue
)

Sets isXRefStream to the given value.

public void
setStartXref(long
the value for startXref
startXrefValue
)

This method set the startxref value of the document.

public void
setTrailer(COSDictionary
the document trailer dictionary
newTrailer
)

// MIT added, maybe this should not be supported as trailer is a persistence construct.

public void
setVersion(float
The version of the PDF document.
versionValue
)

This will set the header version of this PDF document.

Inherited from org.apache.pdfbox.cos.COSBase:
getCOSObjectgetKeyisDirectsetDirectsetKey

Field Detail

closedback to summary
private boolean closed
documentStateback to summary
private final COSDocumentState documentState
hasHybridXRefback to summary
private boolean hasHybridXRef
highestXRefObjectNumberback to summary
private long highestXRefObjectNumber

Used for incremental saving, to avoid XRef object numbers from being reused.

isDecryptedback to summary
private boolean isDecrypted

Signal that document is already decrypted.

isXRefStreamback to summary
private boolean isXRefStream
LOGback to summary
private static final Log LOG

Log instance.

objectPoolback to summary
private final Map<COSObjectKey, COSObject> objectPool

Maps ObjectKeys to a COSObject. Note that references to these objects are also stored in COSDictionary objects that map a name to a specific object.

parserback to summary
private final ICOSParser parser
startXrefback to summary
private long startXref
streamCacheback to summary
private final RandomAccessStreamCache streamCache
streamsback to summary
private final List<COSStream> streams

List containing all streams which are created when creating a new pdf.

trailerback to summary
private COSDictionary trailer

Document trailer dictionary.

versionback to summary
private float version
xrefTableback to summary
private final Map<COSObjectKey, Long> xrefTable

Maps object and generation id to object byte offsets.

Constructor Detail

COSDocumentback to summary
public COSDocument()

Constructor. Uses main memory to buffer PDF streams.

COSDocumentback to summary
public COSDocument(ICOSParser parser)

Constructor. Uses main memory to buffer PDF streams.

Parameters
parser:ICOSParser

Parser to be used to parse the document on demand

COSDocumentback to summary
public COSDocument(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction)

Constructor that will use the provided function to create a stream cache for the storage of the PDF streams.

Parameters
streamCacheCreateFunction:RandomAccessStreamCache.StreamCacheCreateFunction

a function to create an instance of a stream cache

COSDocumentback to summary
public COSDocument(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, ICOSParser parser)

Constructor that will use the provided function to create a stream cache for the storage of the PDF streams.

Parameters
streamCacheCreateFunction:RandomAccessStreamCache.StreamCacheCreateFunction

a function to create an instance of a stream cache

parser:ICOSParser

Parser to be used to parse the document on demand

Method Detail

acceptback to summary
public void accept(ICOSVisitor visitor) throws IOException

Implements abstract org.apache.pdfbox.cos.COSBase.accept.

visitor pattern double dispatch method.

Parameters
visitor:ICOSVisitor

The object to notify when visiting this object.

Annotations
@Override
Exceptions
IOException:
If an error occurs while visiting this object.
addXRefTableback to summary
public void addXRefTable(Map<COSObjectKey, Long> xrefTableValues)

Populate XRef HashMap with given values. Each entry maps ObjectKeys to byte offsets in the file.

Parameters
xrefTableValues:Map<COSObjectKey, Long>

xref table entries to be added

closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

This will close all storage and delete the tmp files.

Annotations
@Override
Exceptions
IOException:
If there is an error close resources.
createCOSStreamback to summary
public COSStream createCOSStream()

Creates a new COSStream using the current configuration for scratch files.

Returns:COSStream

the new COSStream

createCOSStreamback to summary
public COSStream createCOSStream(COSDictionary dictionary, long startPosition, long streamLength) throws IOException

Creates a new COSStream using the current configuration for scratch files. Not for public use. Only COSParser should call this method.

Parameters
dictionary:COSDictionary

the corresponding dictionary

startPosition:long

the start position within the source

streamLength:long

the stream length

Returns:COSStream

the new COSStream

Exceptions
IOException:
if the random access view can't be read
getDocumentIDback to summary
public COSArray getDocumentID()

This will get the document ID.

Returns:COSArray

The document id.

getDocumentStateback to summary
public COSDocumentState getDocumentState()

Returns the COSDocumentState of this COSDocument.

Returns:COSDocumentState

The COSDocumentState of this COSDocument.

See Also
COSDocumentState
getEncryptionDictionaryback to summary
public COSDictionary getEncryptionDictionary()

This will get the encryption dictionary if the document is encrypted or null if the document is not encrypted.

Returns:COSDictionary

The encryption dictionary.

getHighestXRefObjectNumberback to summary
public long getHighestXRefObjectNumber()

Internal PDFBox use only. Get the object number of the highest XRef stream. This is needed to avoid reusing such a number in incremental saving.

Returns:long

The object number of the highest XRef stream, or 0 if there was no XRef stream.

getLinearizedDictionaryback to summary
public COSDictionary getLinearizedDictionary()

Get the dictionary containing the linearization information if the pdf is linearized.

Returns:COSDictionary

the dictionary containing the linearization information

getObjectFromPoolback to summary
public COSObject getObjectFromPool(COSObjectKey key)

This will get an object from the pool.

Parameters
key:COSObjectKey

The object key.

Returns:COSObject

The object in the pool or a new one if it has not been parsed yet.

getObjectsByTypeback to summary
public List<COSObject> getObjectsByType(COSName type)

This will get all dictionaries objects by type.

Parameters
type:COSName

The type of the object.

Returns:List<COSObject>

This will return all objects with the specified type.

getObjectsByTypeback to summary
public List<COSObject> getObjectsByType(COSName type1, COSName type2)

This will get all dictionaries objects by type.

Parameters
type1:COSName

The first possible type of the object, mandatory.

type2:COSName

The second possible type of the object, usually an abbreviation, optional.

Returns:List<COSObject>

This will return all objects with the specified type(s).

getObjectsByTypeback to summary
private List<COSObject> getObjectsByType(List<COSObjectKey> keys, COSName type1, COSName type2)
getStartXrefback to summary
public long getStartXref()

Return the startXref Position of the parsed document. This will only be needed for incremental updates.

Returns:long

a long with the old position of the startxref

getStreamCacheback to summary
private RandomAccessStreamCache getStreamCache(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction)
getTrailerback to summary
public COSDictionary getTrailer()

This will get the document trailer.

Returns:COSDictionary

the document trailer dict

getVersionback to summary
public float getVersion()

This will get the version extracted from the header of this PDF document.

Returns:float

The header version.

getXrefTableback to summary
public Map<COSObjectKey, Long> getXrefTable()

Returns the xrefTable which is a mapping of ObjectKeys to byte offsets in the file.

Returns:Map<COSObjectKey, Long>

mapping of ObjectsKeys to byte offsets

hasHybridXRefback to summary
public boolean hasHybridXRef()

Determines if the pdf has hybrid cross references, both plain tables and streams.

Returns:boolean

true if the pdf has hybrid cross references

isClosedback to summary
public boolean isClosed()

Returns true if this document has been closed.

Returns:boolean

true if the document is already closed, false otherwise

isDecryptedback to summary
public boolean isDecrypted()

Indicates if a encrypted pdf is already decrypted after parsing.

Returns:boolean

true indicates that the pdf is decrypted.

isEncryptedback to summary
public boolean isEncrypted()

This will tell if this is an encrypted document.

Returns:boolean

true If this document is encrypted.

isXRefStreamback to summary
public boolean isXRefStream()

Determines if the trailer is a XRef stream or not.

Returns:boolean

true if the trailer is a XRef stream

setDecryptedback to summary
public void setDecrypted()

Signals that the document is decrypted completely.

setDocumentIDback to summary
public void setDocumentID(COSArray id)

This will set the document ID. This should be an array of two strings. This method cannot be used to remove the document id by passing null or an empty array; it will be recreated. Only the first existing string is used when writing, the second one is always recreated. If you don't want this, you'll have to modify the COSWriter class, look for COSName#ID.

Parameters
id:COSArray

The document id.

setEncryptionDictionaryback to summary
public void setEncryptionDictionary(COSDictionary encDictionary)

This will set the encryption dictionary, this should only be called when encrypting the document.

Parameters
encDictionary:COSDictionary

The encryption dictionary.

setHasHybridXRefback to summary
public void setHasHybridXRef()

Marks the pdf as document using hybrid cross references.

setHighestXRefObjectNumberback to summary
public void setHighestXRefObjectNumber(long highestXRefObjectNumber)

Internal PDFBox use only. Sets the object number of the highest XRef stream. This is needed to avoid reusing such a number in incremental saving.

Parameters
highestXRefObjectNumber:long

The object number of the highest XRef stream.

setIsXRefStreamback to summary
public void setIsXRefStream(boolean isXRefStreamValue)

Sets isXRefStream to the given value. You need to take care that the version of your PDF is 1.5 or higher.

Parameters
isXRefStreamValue:boolean

the new value for isXRefStream

setStartXrefback to summary
public void setStartXref(long startXrefValue)

This method set the startxref value of the document. This will only be needed for incremental updates.

Parameters
startXrefValue:long

the value for startXref

setTrailerback to summary
public void setTrailer(COSDictionary newTrailer)

// MIT added, maybe this should not be supported as trailer is a persistence construct. This will set the document trailer.

Parameters
newTrailer:COSDictionary

the document trailer dictionary

setVersionback to summary
public void setVersion(float versionValue)

This will set the header version of this PDF document.

Parameters
versionValue:float

The version of the PDF document.