org.apache.pdfbox.cos
Class COSDocument

java.lang.Object
  extended by org.apache.pdfbox.cos.COSBase
      extended by org.apache.pdfbox.cos.COSDocument
All Implemented Interfaces:
COSObjectable

public class COSDocument
extends COSBase

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

Version:
$Revision: 1.28 $
Author:
Ben Litchfield

Constructor Summary
COSDocument()
          Constructor.
COSDocument(File scratchDir)
          Constructor that will create a create a scratch file in the following directory.
COSDocument(RandomAccess file)
          Constructor that will use the following random access file for storage of the PDF streams.
 
Method Summary
 Object accept(ICOSVisitor visitor)
          visitor pattern double dispatch method.
 void close()
          This will close all storage and delete the tmp files.
 void dereferenceObjectStreams()
          This method will search the list of objects for types of ObjStm.
protected  void finalize()
          Warn the user in the finalizer if he didn't close the PDF document.
 COSObject getCatalog()
          This will get the document catalog.
 COSArray getDocumentID()
          This will get the document ID.
 COSDictionary getEncryptionDictionary()
          This will get the encryption dictionary if the document is encrypted or null if the document is not encrypted.
 String getHeaderString()
           
 COSObject getObjectByType(COSName type)
          This will get the first dictionary object by type.
 COSObject getObjectByType(String type)
          This will get the first dictionary object by type.
 COSObject getObjectFromPool(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.
 RandomAccess getScratchFile()
          This will get the scratch file for this document.
 COSDictionary getTrailer()
          This will get the document trailer.
 float getVersion()
          This will get the version of this PDF document.
 Map<COSObjectKey,Integer> getXrefTable()
          Returns the xrefTable which is a mapping of ObjectKeys to byte offsets in the file.
 boolean isEncrypted()
          This will tell if this is an encrypted document.
 void parseXrefStreams()
          This method will search the list of objects for types of XRef and uses the parsed data to populate the trailer information as well as the xref Map.
 void print()
          This will print contents to stdout.
 void setDocumentID(COSArray id)
          This will set the document ID.
 void setEncryptionDictionary(COSDictionary encDictionary)
          This will set the encryption dictionary, this should only be called when encypting the document.
 void setHeaderString(String header)
           
 void setTrailer(COSDictionary newTrailer)
          // MIT added, maybe this should not be supported as trailer is a persistence construct.
 void setVersion(float versionValue)
          This will set the version of this PDF document.
 void setWarnMissingClose(boolean warn)
          Controls whether this instance shall issue a warning if the PDF document wasn't closed properly through a call to the close() method.
 void setXRef(COSObjectKey objKey, int offset)
          Used to populate the XRef HashMap.
 
Methods inherited from class org.apache.pdfbox.cos.COSBase
getCOSObject, getFilterManager
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

COSDocument

public COSDocument()
            throws IOException
Constructor. Uses the java.io.tmpdir value to create a file to store the streams.

Throws:
IOException - If there is an error creating the tmp file.

COSDocument

public COSDocument(File scratchDir)
            throws IOException
Constructor that will create a create a scratch file in the following directory.

Parameters:
scratchDir - The directory to store a scratch file.
Throws:
IOException - If there is an error creating the tmp file.

COSDocument

public COSDocument(RandomAccess file)
Constructor that will use the following random access file for storage of the PDF streams. The client of this method is responsible for deleting the storage if necessary that this file will write to. The close method will close the file though.

Parameters:
file - The random access file to use for storage.
Method Detail

getScratchFile

public RandomAccess getScratchFile()
This will get the scratch file for this document.

Returns:
The scratch file.

getObjectByType

public COSObject getObjectByType(String 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

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

print

public void print()
This will print contents to stdout.


setVersion

public void setVersion(float versionValue)
This will set the version of this PDF document.

Parameters:
versionValue - The version of the PDF document.

getVersion

public float getVersion()
This will get the version of this PDF document.

Returns:
This documents version.

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 encypting 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 COSVisitorException
visitor pattern double dispatch method.

Specified by:
accept in class COSBase
Parameters:
visitor - The object to notify when visiting this object.
Returns:
any object, depending on the visitor implementation, or null
Throws:
COSVisitorException - If an error occurs while visiting this object.

close

public void close()
           throws IOException
This will close all storage and delete the tmp files.

Throws:
IOException - If there is an error close resources.

finalize

protected void finalize()
                 throws IOException
Warn 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:
finalize in class Object
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 the close() 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.

getHeaderString

public String getHeaderString()
Returns:
Returns the headerString.

setHeaderString

public void setHeaderString(String header)
Parameters:
header - The headerString to set.

dereferenceObjectStreams

public void dereferenceObjectStreams()
                              throws IOException
This 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.

setXRef

public void setXRef(COSObjectKey objKey,
                    int offset)
Used to populate the XRef HashMap. Will add an Xreftable entry that maps ObjectKeys to byte offsets in the file.

Parameters:
objKey - The objkey, with id and gen numbers
offset - The byte offset in this file

getXrefTable

public Map<COSObjectKey,Integer> getXrefTable()
Returns the xrefTable which is a mapping of ObjectKeys to byte offsets in the file.

Returns:
mapping of ObjectsKeys to byte offsets

parseXrefStreams

public void parseXrefStreams()
                      throws IOException
This method will search the list of objects for types of XRef and uses the parsed data to populate the trailer information as well as the xref Map.

Throws:
IOException - if there is an error parsing the stream


Copyright © 2002-2010 The Apache Software Foundation. All Rights Reserved.