Package com.tom_roush.pdfbox.cos
Class COSDocument
- java.lang.Object
-
- com.tom_roush.pdfbox.cos.COSBase
-
- com.tom_roush.pdfbox.cos.COSDocument
-
- All Implemented Interfaces:
COSObjectable,Closeable,AutoCloseable
public class COSDocument extends COSBase implements Closeable
This is the in-memory representation of the PDF document. You need to call close() on this object when you are done using it!!
-
-
Constructor Summary
Constructors Constructor Description COSDocument()Constructor.COSDocument(boolean useScratchFiles)Constructor.COSDocument(ScratchFile scratchFile)Constructor that will use the provide memory handler for storage of the PDF streams.COSDocument(File scratchDir, boolean useScratchFiles)Constructor that will use a temporary file in the given directory for storage of the PDF streams.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectaccept(ICOSVisitor visitor)visitor pattern double dispatch method.voidaddXRefTable(Map<COSObjectKey,Long> xrefTableValues)Populate XRef HashMap with given values.voidclose()This will close all storage and delete the tmp files.COSStreamcreateCOSStream()Creates a new COSStream using the current configuration for scratch files.COSStreamcreateCOSStream(COSDictionary dictionary)Creates a new COSStream using the current configuration for scratch files.voiddereferenceObjectStreams()This method will search the list of objects for types of ObjStm.protected voidfinalize()Warn the user in the finalizer if he didn't close the PDF document.COSObjectgetCatalog()This will get the document catalog.COSArraygetDocumentID()This will get the document ID.COSDictionarygetEncryptionDictionary()This will get the encryption dictionary if the document is encrypted or null if the document is not encrypted.COSObjectKeygetKey(COSBase object)Returns the COSObjectKey for a given COS object, or null if there is none.COSObjectgetObjectByType(COSName type)This will get the first dictionary object by type.COSObjectgetObjectFromPool(COSObjectKey key)This will get an object from the pool.List<COSObject>getObjects()This will get a list of all available objects.List<COSObject>getObjectsByType(COSName type)This will get a dictionary object by type.List<COSObject>getObjectsByType(String type)This will get all dictionary objects by type.longgetStartXref()Return the startXref Position of the parsed document.COSDictionarygetTrailer()This will get the document trailer.floatgetVersion()This will get the version extracted from the header of this PDF document.Map<COSObjectKey,Long>getXrefTable()Returns the xrefTable which is a mapping of ObjectKeys to byte offsets in the file.booleanisClosed()Returns true if this document has been closed.booleanisDecrypted()Indicates if a encrypted pdf is already decrypted after parsing.booleanisEncrypted()This will tell if this is an encrypted document.booleanisXRefStream()Determines if the trailer is a XRef stream or not.voidprint()This will print contents to stdout.COSObjectremoveObject(COSObjectKey key)Removes an object from the object pool.voidsetDecrypted()Signals that the document is decrypted completely.voidsetDocumentID(COSArray id)This will set the document ID.voidsetEncryptionDictionary(COSDictionary encDictionary)This will set the encryption dictionary, this should only be called when encrypting the document.voidsetIsXRefStream(boolean isXRefStreamValue)Sets isXRefStream to the given value.voidsetStartXref(long startXrefValue)This method set the startxref value of the document.voidsetTrailer(COSDictionary newTrailer)// MIT added, maybe this should not be supported as trailer is a persistence construct.voidsetVersion(float versionValue)This will set the header version of this PDF document.voidsetWarnMissingClose(boolean warn)Controls whether this instance shall issue a warning if the PDF document wasn't closed properly through a call to theclose()method.-
Methods inherited from class com.tom_roush.pdfbox.cos.COSBase
getCOSObject, isDirect, setDirect
-
-
-
-
Constructor Detail
-
COSDocument
public COSDocument(boolean useScratchFiles)
Constructor.- Parameters:
useScratchFiles- enables the usage of a scratch file if set to true
-
COSDocument
public COSDocument(File scratchDir, boolean useScratchFiles)
Constructor that will use a temporary file in the given directory for storage of the PDF streams. The temporary file is automatically removed when this document gets closed.- Parameters:
scratchDir- directory for the temporary file, ornullto use the system defaultuseScratchFiles- enables the usage of a scratch file if set to true
-
COSDocument
public COSDocument(ScratchFile scratchFile)
Constructor that will use the provide memory handler for storage of the PDF streams.- Parameters:
scratchFile- memory handler for storage of PDF streams
-
COSDocument
public COSDocument()
Constructor. Uses memory to store stream.
-
-
Method Detail
-
createCOSStream
public COSStream createCOSStream()
Creates a new COSStream using the current configuration for scratch files.- Returns:
- the new COSStream
-
createCOSStream
public COSStream createCOSStream(COSDictionary dictionary)
Creates a new COSStream using the current configuration for scratch files. Not for public use. Only COSParser should call this method.- Parameters:
dictionary- the corresponding dictionary- Returns:
- the new COSStream
-
getObjectByType
public COSObject getObjectByType(COSName type) throws IOException
This will get the first dictionary object by type.- Parameters:
type- The type of the object.- Returns:
- This will return an object with the specified type.
- Throws:
IOException- If there is an error getting the object
-
getObjectsByType
public List<COSObject> getObjectsByType(String type) throws IOException
This will get all dictionary objects by type.- Parameters:
type- The type of the object.- Returns:
- This will return an object with the specified type.
- Throws:
IOException- If there is an error getting the object
-
getObjectsByType
public List<COSObject> getObjectsByType(COSName type) throws IOException
This will get a dictionary object by type.- Parameters:
type- The type of the object.- Returns:
- This will return an object with the specified type.
- Throws:
IOException- If there is an error getting the object
-
getKey
public COSObjectKey getKey(COSBase object)
Returns the COSObjectKey for a given COS object, or null if there is none. This lookup iterates over all objects in a PDF, which may be slow for large files.- Parameters:
object- COS object- Returns:
- key
-
print
public void print()
This will print contents to stdout.
-
setVersion
public void setVersion(float versionValue)
This will set the header version of this PDF document.- Parameters:
versionValue- The version of the PDF document.
-
getVersion
public float getVersion()
This will get the version extracted from the header of this PDF document.- Returns:
- The header version.
-
setDecrypted
public void setDecrypted()
Signals that the document is decrypted completely.
-
isDecrypted
public boolean isDecrypted()
Indicates if a encrypted pdf is already decrypted after parsing.- Returns:
- true indicates that the pdf is decrypted.
-
isEncrypted
public boolean isEncrypted()
This will tell if this is an encrypted document.- Returns:
- true If this document is encrypted.
-
getEncryptionDictionary
public COSDictionary getEncryptionDictionary()
This will get the encryption dictionary if the document is encrypted or null if the document is not encrypted.- Returns:
- The encryption dictionary.
-
setEncryptionDictionary
public void setEncryptionDictionary(COSDictionary encDictionary)
This will set the encryption dictionary, this should only be called when encrypting the document.- Parameters:
encDictionary- The encryption dictionary.
-
getDocumentID
public COSArray getDocumentID()
This will get the document ID.- Returns:
- The document id.
-
setDocumentID
public void setDocumentID(COSArray id)
This will set the document ID.- Parameters:
id- The document id.
-
getCatalog
public COSObject getCatalog() throws IOException
This will get the document catalog. Maybe this should move to an object at PDFEdit level- Returns:
- catalog is the root of all document activities
- Throws:
IOException- If no catalog can be found.
-
getObjects
public List<COSObject> getObjects()
This will get a list of all available objects.- Returns:
- A list of all objects.
-
getTrailer
public COSDictionary getTrailer()
This will get the document trailer.- Returns:
- the document trailer dict
-
setTrailer
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- the document trailer dictionary
-
accept
public Object accept(ICOSVisitor visitor) throws IOException
visitor pattern double dispatch method.- Specified by:
acceptin classCOSBase- Parameters:
visitor- The object to notify when visiting this object.- Returns:
- any object, depending on the visitor implementation, or null
- Throws:
IOException- If an error occurs while visiting this object.
-
close
public void close() throws IOExceptionThis will close all storage and delete the tmp files.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- If there is an error close resources.
-
isClosed
public boolean isClosed()
Returns true if this document has been closed.
-
finalize
protected void finalize() throws IOExceptionWarn the user in the finalizer if he didn't close the PDF document. The method also closes the document just in case, to avoid abandoned temporary files. It's still a good idea for the user to close the PDF document at the earliest possible to conserve resources.- Overrides:
finalizein classObject- Throws:
IOException- if an error occurs while closing the temporary files
-
setWarnMissingClose
public void setWarnMissingClose(boolean warn)
Controls whether this instance shall issue a warning if the PDF document wasn't closed properly through a call to theclose()method. If the PDF document is held in a cache governed by soft references it is impossible to reliably close the document before the warning is raised. By default, the warning is enabled.- Parameters:
warn- true enables the warning, false disables it.
-
dereferenceObjectStreams
public void dereferenceObjectStreams() throws IOExceptionThis method will search the list of objects for types of ObjStm. If it finds them then it will parse out all of the objects from the stream that is contains.- Throws:
IOException- If there is an error parsing the stream.
-
getObjectFromPool
public COSObject getObjectFromPool(COSObjectKey key) throws IOException
This will get an object from the pool.- Parameters:
key- The object key.- Returns:
- The object in the pool or a new one if it has not been parsed yet.
- Throws:
IOException- If there is an error getting the proxy object.
-
removeObject
public COSObject removeObject(COSObjectKey key)
Removes an object from the object pool.- Parameters:
key- the object key- Returns:
- the object that was removed or null if the object was not found
-
addXRefTable
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- xref table entries to be added
-
getXrefTable
public Map<COSObjectKey,Long> getXrefTable()
Returns the xrefTable which is a mapping of ObjectKeys to byte offsets in the file.- Returns:
- mapping of ObjectsKeys to byte offsets
-
setStartXref
public void setStartXref(long startXrefValue)
This method set the startxref value of the document. This will only be needed for incremental updates.- Parameters:
startXrefValue- the value for startXref
-
getStartXref
public long getStartXref()
Return the startXref Position of the parsed document. This will only be needed for incremental updates.- Returns:
- a long with the old position of the startxref
-
isXRefStream
public boolean isXRefStream()
Determines if the trailer is a XRef stream or not.- Returns:
- true if the trailer is a XRef stream
-
setIsXRefStream
public void setIsXRefStream(boolean isXRefStreamValue)
Sets isXRefStream to the given value.- Parameters:
isXRefStreamValue- the new value for isXRefStream
-
-