com.etymon.pjx
Class PdfManager

java.lang.Object
  extended by com.etymon.pjx.PdfManager

public final class PdfManager
extends java.lang.Object

Manages the modification or creation of a PDF document. This class is synchronized.


Field Summary
protected  long _last_startxref
           
protected  java.util.Map _modobj
          The collection of PDF objects that have been modified.
protected  int _modsize
          The total size of the cross-reference table, taking together both the PDF input document and the modified objects.
protected  PdfReader _reader
          The PDF input document.
protected  long _startxref
          The beginning offset of the cross-reference table in the PDF input document.
protected  PdfDictionary _trailer
          The document's trailer dictionary, in its current (possibly modified) state.
protected  XrefTable _xt
          The cross-reference table and associated trailer read from the PDF input document.
protected static PdfName PDFNAME_PREV
           
protected static PdfName PDFNAME_SIZE
           
 
Constructor Summary
PdfManager(boolean init)
          Constructs a PdfManager representing an empty PDF document.
PdfManager(PdfReader pdfReader)
          Constructs a PdfManager representing an existing PDF document.
 
Method Summary
 int addObject(PdfObject obj)
          Adds a PDF object to the document and assigns a new object number to it.
protected  void construct()
          Performs initialization common to multiple constructors of this class.
 PdfObject getObject(int objectNumber)
          Retrieves the PDF object associated with a specified object number.
 PdfObject getObjectIndirect(PdfObject obj)
          Retrieves the PDF object referred to by a specified indirect reference object.
 PdfReader getReader()
          Returns the PdfReader instance associated with this document.
 long getStartxref()
          Deprecated. Do not use this method.
 PdfDictionary getTrailerDictionary()
          Returns the document's trailer dictionary.
 int getXrefTableSize()
          Returns the current size of the cross-reference table, taking into account any modifications that have been made to the document via this PdfManager.
private  void initialize()
          Creates the minimal set of objects in a newly constructed document in order to create a single blank page.
 void setObject(PdfObject obj, int objectNumber)
          Adds a PDF object to the document and assigns the specified object number to it.
 void setTrailerDictionary(PdfDictionary dictionary)
          Assigns the document a new trailer dictionary, replacing the existing one (if any).
 long writeDocument(PdfWriter pdfWriter)
          Writes the document in PDF format, including all modifications made through this PdfManager.
 long writeDocument(PdfWriter pdfWriter, boolean useIncrementalUpdate)
          Writes the document in PDF format, including all modifications made through this PdfManager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_last_startxref

protected long _last_startxref

PDFNAME_PREV

protected static final PdfName PDFNAME_PREV

PDFNAME_SIZE

protected static final PdfName PDFNAME_SIZE

_modobj

protected java.util.Map _modobj
The collection of PDF objects that have been modified. Each PDF object is keyed by its associated object number (stored as type Integer).


_modsize

protected int _modsize
The total size of the cross-reference table, taking together both the PDF input document and the modified objects. This is also used as the next free object number.


_reader

protected PdfReader _reader
The PDF input document.


_startxref

protected long _startxref
The beginning offset of the cross-reference table in the PDF input document.


_trailer

protected PdfDictionary _trailer
The document's trailer dictionary, in its current (possibly modified) state.


_xt

protected XrefTable _xt
The cross-reference table and associated trailer read from the PDF input document.

Constructor Detail

PdfManager

public PdfManager(boolean init)
Constructs a PdfManager representing an empty PDF document. The document in this initial state contains no objects and therefore does not correspond to a valid PDF file.

Parameters:
init - if false, specifies that the document contain no objects (and therefore will not correspond to a valid PDF file); if true, that the document be initialized with a minimal set of objects to create a single blank page.

PdfManager

public PdfManager(PdfReader pdfReader)
           throws java.io.IOException,
                  PdfFormatException
Constructs a PdfManager representing an existing PDF document.

Parameters:
pdfReader - specifies the existing PDF document to read.
Throws:
java.io.IOException
PdfFormatException
Method Detail

getStartxref

public long getStartxref()
Deprecated. Do not use this method.


getReader

public PdfReader getReader()
Returns the PdfReader instance associated with this document.


initialize

private void initialize()
Creates the minimal set of objects in a newly constructed document in order to create a single blank page. This method assumes that the document contains no objects. (This method is adapted from com.etymon.pj.Pdf.createEmpty().)


addObject

public int addObject(PdfObject obj)
Adds a PDF object to the document and assigns a new object number to it.

Parameters:
obj - the PDF object to be added.
Returns:
the new object number assigned to the PDF object. The generation number of the object will be 0.

getObject

public PdfObject getObject(int objectNumber)
                    throws java.io.IOException,
                           PdfFormatException
Retrieves the PDF object associated with a specified object number.

Parameters:
objectNumber - specifies the object number of the PDF object to be retrieved.
Returns:
the retrieved PDF object.
Throws:
java.io.IOException
PdfFormatException

getObjectIndirect

public PdfObject getObjectIndirect(PdfObject obj)
                            throws java.io.IOException,
                                   PdfFormatException
Retrieves the PDF object referred to by a specified indirect reference object. If the specified object is not an indirect reference but a direct object, that object itself is returned. If the specified object is null, then null is returned.

Parameters:
obj - the indirect reference object (or direct object, or null).
Returns:
the retrieved object (or direct object).
Throws:
java.io.IOException
PdfFormatException

getTrailerDictionary

public PdfDictionary getTrailerDictionary()
Returns the document's trailer dictionary.

Returns:
the trailer dictionary.

getXrefTableSize

public int getXrefTableSize()
Returns the current size of the cross-reference table, taking into account any modifications that have been made to the document via this PdfManager.

Returns:
the size of the cross-reference table.

construct

protected void construct()
Performs initialization common to multiple constructors of this class. This method is only intended to be called from the constructors.


setObject

public void setObject(PdfObject obj,
                      int objectNumber)
Adds a PDF object to the document and assigns the specified object number to it. If an object in the document is already assigned to the specified object number, that object is replaced with the new one.

Parameters:
obj - the PDF object to be added.
objectNumber - the object number to assign to the PDF object.

setTrailerDictionary

public void setTrailerDictionary(PdfDictionary dictionary)
Assigns the document a new trailer dictionary, replacing the existing one (if any).

Parameters:
dictionary - specifies the new trailer dictionary.

writeDocument

public long writeDocument(PdfWriter pdfWriter)
                   throws java.io.IOException,
                          PdfFormatException
Writes the document in PDF format, including all modifications made through this PdfManager. This method is equivalent to using writeDocument(..., true); i.e. it uses incremental update if applicable. Note that this means the resultant document will be larger than the original document; otherwise use writeDocument(..., false) to get a smaller resultant file, although it will usually take longer to generate. The PdfWriter should be newly created (i.e. it should not have been previously used for anything); and after this method has been called, the PdfWriter should be closed and discarded.

Parameters:
pdfWriter - specifies the PDF document to write.
Returns:
the number of bytes written.
Throws:
java.io.IOException
PdfFormatException

writeDocument

public long writeDocument(PdfWriter pdfWriter,
                          boolean useIncrementalUpdate)
                   throws java.io.IOException,
                          PdfFormatException
Writes the document in PDF format, including all modifications made through this PdfManager. This method will optionally use PDF's incremental update format, which often takes significantly less processing time but creates a larger resultant PDF file and may be slower for a reader to open (if the file has been updated many times in this way). Incremental update should not be used when the original document is a Linearized PDF file if changes have been made that would invalidate its correctness, unless the resultant PDF file will not be used in an application that depends on its correct Linearization. If this PdfManager represents a new document rather than modifying an existing one, then the incremental update option is not applicable and is disregarded. The PdfWriter should be newly created (i.e. it should not have been previously used for anything); and after this method has been called, the PdfWriter should be closed and discarded.

Parameters:
pdfWriter - specifies the PDF document to write.
useIncrementalUpdate - specifies whether incremental update format should be used. A value of true enables incremental update.
Returns:
the number of bytes written.
Throws:
java.io.IOException
PdfFormatException