com.etymon.pj
Class Pdf

java.lang.Object
  extended by com.etymon.pj.Pdf

public class Pdf
extends java.lang.Object

A document representation of a PDF file.


Field Summary
protected  PjObjectVector _objects
           
protected  java.util.Hashtable _trailer
           
 
Constructor Summary
Pdf()
          Creates an empty PDF document.
Pdf(java.lang.String filename)
          Creates a PDF document from an existing PDF file.
 
Method Summary
 void addToPage(PjPage page, int objectNumber)
          Adds a PjObject to a page in this PDF document.
private static void adjustTextMatrixX(java.util.Vector pmVector, float rectWidth)
           
 int appendPage(int objectNumber)
          Appends a PjPage to the end of this PDF document.
 void appendPdfDocument(Pdf pdf)
          Appends the pages of a PDF document to this document.
private static void clearTextMatrixX(java.util.Vector pmVector)
           
private  void createEmpty()
           
 int deletePage(int pageNumber)
          Deletes a page in this document by page number.
private  int findPage(int pageNumber, int objectNumber, PjPages parentPages, IntCounter counter, boolean delete)
           
 int getCatalog()
          Looks up the Catalog object in this document.
 PjDictionary getEncryptDictionary()
          Looks up the Encrypt dictionary within this document's trailer.
 java.util.Vector getFields()
           
private  void getFieldsAddField(java.util.Vector fieldList, PjReference fieldRef)
           
 PjReference getInfoDictionary()
          Looks up the Info dictionary within this document's trailer.
 int getMaxObjectNumber()
          Returns the largest object number in the list of registered PjObjects.
 PjObject getObject(int objectNumber)
          Looks up a PjObject by its object number.
 int getPage(int pageNumber)
          Looks up a page in this document by page number.
 int getPageCount()
          Determines the number of pages in this PDF document.
 int getRootPages()
          Looks up the root Pages object of this document's Pages tree.
 PjDictionary inheritFieldAttributes(PjDictionary node)
          Returns a clone of a field node such that all inherited attributes of the given field node are made explicit.
private  void inheritFieldAttributesCollapse(PjName name, java.util.Hashtable ht, PjDictionary newNode, PjDictionary parent)
           
 PjPagesNode inheritPageAttributes(PjPagesNode node)
          Returns a clone of a pages node such that all inherited attributes of the given pages node are made explicit.
private  void inheritPageAttributesCollapse(PjName name, java.util.Hashtable ht, PjPagesNode newNode, PjPagesNode parent)
           
private  void init()
           
private  void readFromFile(java.lang.String filename)
           
 int registerObject(PjObject obj)
          Registers a PjObject within this PDF document.
 void registerObject(PjObject obj, int objectNumber)
          Registers a PjObject within this PDF document using a specified object number.
private static void replaceTextData(java.util.Vector pmVector, PjString oldText, PjString newText)
           
 PjObject resolve(PjObject obj)
          Dereferences a PjObject if it is a PjReference.
 void setEncryptDictionary(PjDictionary dict)
          Sets the Encrypt dictionary within this document's trailer.
 void setEncryptDictionary(PjReference ref)
          Sets the Encrypt dictionary within this document's trailer.
 void setInfoDictionary(PjReference ref)
          Sets the Info dictionary within this document's trailer.
 void updateFieldValue(PjDictionary origField, PjDictionary field, java.lang.String value)
           
 void writeToFile(java.lang.String filename)
          Writes this PDF document to a file in PDF format.
 void writeToStream(java.io.OutputStream os)
          Writes this PDF document to a stream in PDF format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_objects

protected PjObjectVector _objects

_trailer

protected java.util.Hashtable _trailer
Constructor Detail

Pdf

public Pdf()
Creates an empty PDF document.


Pdf

public Pdf(java.lang.String filename)
    throws java.io.IOException,
           PjException
Creates a PDF document from an existing PDF file.

Parameters:
filename - the name of the PDF file to read.
Throws:
java.io.IOException - if an I/O error occurs.
PjException - if a PDF error occurs.
Method Detail

writeToFile

public void writeToFile(java.lang.String filename)
                 throws java.io.IOException
Writes this PDF document to a file in PDF format.

Parameters:
filename - the name of the PDF file to create.
Throws:
java.io.IOException - if an I/O error occurs.

writeToStream

public void writeToStream(java.io.OutputStream os)
                   throws java.io.IOException
Writes this PDF document to a stream in PDF format.

Parameters:
os - the stream to write to.
Throws:
java.io.IOException - if an I/O error occurs.

registerObject

public int registerObject(PjObject obj)
Registers a PjObject within this PDF document.

Parameters:
obj - the PjObject to register.
Returns:
the new object number of the registered PjObject.

registerObject

public void registerObject(PjObject obj,
                           int objectNumber)
Registers a PjObject within this PDF document using a specified object number.

Parameters:
obj - the PjObject to register.
objectNumber - the object number to register obj under.

addToPage

public void addToPage(PjPage page,
                      int objectNumber)
               throws InvalidPdfObjectException
Adds a PjObject to a page in this PDF document.

Parameters:
page - the page object to add to.
objectNumber - the object number of the PjObject to add.
Throws:
InvalidPdfObjectException - if an invalid object type is encountered.

getObject

public PjObject getObject(int objectNumber)
Looks up a PjObject by its object number.

Parameters:
objectNumber - the object number of the PjObject to retrieve.
Returns:
the requested PjObject.

resolve

public PjObject resolve(PjObject obj)
Dereferences a PjObject if it is a PjReference.

Parameters:
obj - the PjObject to dereference.
Returns:
the referenced PjObject if obj is a PjReference, or obj otherwise.

getPageCount

public int getPageCount()
                 throws InvalidPdfObjectException
Determines the number of pages in this PDF document.

Returns:
the number of pages in this PDF document.
Throws:
InvalidPdfObjectException - if an invalid object type is encountered.

findPage

private int findPage(int pageNumber,
                     int objectNumber,
                     PjPages parentPages,
                     IntCounter counter,
                     boolean delete)
              throws InvalidPdfObjectException
Throws:
InvalidPdfObjectException

getPage

public int getPage(int pageNumber)
            throws java.lang.IndexOutOfBoundsException,
                   InvalidPdfObjectException
Looks up a page in this document by page number.

Parameters:
pageNumber - the page number. Pages are numbered starting with 1.
Returns:
the object number of the identified Page object.
Throws:
java.lang.IndexOutOfBoundsException - if an invalid page number was given.
InvalidPdfObjectException - if an invalid object type is encountered.

deletePage

public int deletePage(int pageNumber)
               throws java.lang.IndexOutOfBoundsException,
                      InvalidPdfObjectException
Deletes a page in this document by page number. The page is deleted by removing the reference to it from the page tree; however, no objects are actually deleted from the document.

Parameters:
pageNumber - the page number. Pages are numbered starting with 1.
Returns:
the object number of the deleted Page object.
Throws:
java.lang.IndexOutOfBoundsException - if an invalid page number was given.
InvalidPdfObjectException - if an invalid object type is encountered.

appendPage

public int appendPage(int objectNumber)
Appends a PjPage to the end of this PDF document.

Parameters:
objectNumber - the object number of the PjPage to append.
Returns:
the new object number of the appended PjPage.

appendPdfDocument

public void appendPdfDocument(Pdf pdf)
                       throws InvalidPdfObjectException
Appends the pages of a PDF document to this document. Note that this does not clone the other document but simply includes references to its objects. Therefore the other document should be discarded immediately after a call to this method, otherwise you could get very strange results.

Parameters:
pdf - the PDF document to append.
Throws:
InvalidPdfObjectException - if an invalid object type is encountered in either document.

getCatalog

public int getCatalog()
               throws InvalidPdfObjectException
Looks up the Catalog object in this document.

Returns:
the object number of the Catalog object.
Throws:
InvalidPdfObjectException - if an invalid object type is encountered.

getRootPages

public int getRootPages()
                 throws InvalidPdfObjectException
Looks up the root Pages object of this document's Pages tree.

Returns:
the object number of the root Pages object.
Throws:
InvalidPdfObjectException - if an invalid object type is encountered.

getInfoDictionary

public PjReference getInfoDictionary()
                              throws InvalidPdfObjectException
Looks up the Info dictionary within this document's trailer. The Info dictionary contains general information about the document.

Returns:
a reference to the Info dictionary, or null if no Info field is present in the trailer.
Throws:
InvalidPdfObjectException - if the Info field in the trailer is not a reference (PjReference) object.

setInfoDictionary

public void setInfoDictionary(PjReference ref)
Sets the Info dictionary within this document's trailer.

Parameters:
ref - a reference to the Info dictionary.

getEncryptDictionary

public PjDictionary getEncryptDictionary()
                                  throws InvalidPdfObjectException
Looks up the Encrypt dictionary within this document's trailer. The Encrypt dictionary contains information for decrypting a document.

Returns:
the Encrypt dictionary, or null if no Encrypt field is present in the trailer.
Throws:
InvalidPdfObjectException - if the Encrypt field in the trailer is not a dictionary (PjDictionary) object.

setEncryptDictionary

public void setEncryptDictionary(PjReference ref)
Sets the Encrypt dictionary within this document's trailer.

Parameters:
ref - a reference to the Encrypt dictionary.

setEncryptDictionary

public void setEncryptDictionary(PjDictionary dict)
Sets the Encrypt dictionary within this document's trailer.

Parameters:
dict - the Encrypt dictionary.

inheritPageAttributes

public PjPagesNode inheritPageAttributes(PjPagesNode node)
                                  throws InvalidPdfObjectException
Returns a clone of a pages node such that all inherited attributes of the given pages node are made explicit. For example, if MediaBox is not defined in the given pages node, this method ascends the pages tree (via the Parent reference) looking for an ancestor node that does contain a value for MediaBox; if it finds one, it assigns that value in the cloned (returned) pages node. This is done for all inheritable attributes.

Parameters:
node - a pages node for which inherited attributes are to be retrieved.
Returns:
a cloned copy of the given pages node with actual values substituted for all inherited attributes.
Throws:
InvalidPdfObjectException - if an invalid object type is encountered.

inheritFieldAttributes

public PjDictionary inheritFieldAttributes(PjDictionary node)
                                    throws InvalidPdfObjectException
Returns a clone of a field node such that all inherited attributes of the given field node are made explicit. For example, if the V key is not defined in the given field node, this method ascends the field tree (via the Parent reference) looking for an ancestor node that does contain a value for the V key; if it finds one, it assigns that value in the cloned (returned) field node. This is done for all inheritable attributes.

Parameters:
node - a field node for which inherited attributes are to be retrieved.
Returns:
a cloned copy of the given field node with actual values substituted for all inherited attributes.
Throws:
InvalidPdfObjectException - if an invalid object type is encountered.

getMaxObjectNumber

public int getMaxObjectNumber()
Returns the largest object number in the list of registered PjObjects. This is useful mainly for functions that need to run through the list and process each object, because this provides the maximum object number they need to examine. The object number may not currently be assigned to an object, but probably was at some point in the past.

Returns:
the size of the object list.

getFields

public java.util.Vector getFields()
                           throws InvalidPdfObjectException
Throws:
InvalidPdfObjectException

getFieldsAddField

private void getFieldsAddField(java.util.Vector fieldList,
                               PjReference fieldRef)
                        throws InvalidPdfObjectException
Throws:
InvalidPdfObjectException

updateFieldValue

public void updateFieldValue(PjDictionary origField,
                             PjDictionary field,
                             java.lang.String value)
                      throws PdfFormatException,
                             InvalidPdfObjectException
Throws:
PdfFormatException
InvalidPdfObjectException

replaceTextData

private static void replaceTextData(java.util.Vector pmVector,
                                    PjString oldText,
                                    PjString newText)

adjustTextMatrixX

private static void adjustTextMatrixX(java.util.Vector pmVector,
                                      float rectWidth)

clearTextMatrixX

private static void clearTextMatrixX(java.util.Vector pmVector)

inheritPageAttributesCollapse

private void inheritPageAttributesCollapse(PjName name,
                                           java.util.Hashtable ht,
                                           PjPagesNode newNode,
                                           PjPagesNode parent)

inheritFieldAttributesCollapse

private void inheritFieldAttributesCollapse(PjName name,
                                            java.util.Hashtable ht,
                                            PjDictionary newNode,
                                            PjDictionary parent)

init

private void init()

createEmpty

private void createEmpty()

readFromFile

private void readFromFile(java.lang.String filename)
                   throws java.io.IOException,
                          PjException
Throws:
java.io.IOException
PjException