org.pdfbox.pdmodel
Class PDDocument

java.lang.Object
  extended byorg.pdfbox.pdmodel.PDDocument

public class PDDocument
extends Object

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.24 $
Author:
Ben Litchfield (ben@csh.rit.edu)

Constructor Summary
PDDocument()
          Constructor, creates a new PDF Document with no pages.
PDDocument(COSDocument doc)
          Constructor that uses an existing document.
 
Method Summary
 void addPage(PDPage page)
          This will add a page to the document.
 void close()
          This will close the underlying COSDocument object.
 void decrypt(String password)
          This will decrypt a document.
 void encrypt(String ownerPassword, String userPassword)
          This will encrypt a document.
 COSDocument getDocument()
          This will get the low level document.
 PDDocumentCatalog getDocumentCatalog()
          This will get the document CATALOG.
 PDDocumentInformation getDocumentInformation()
          This will get the document info dictionary.
 PDEncryptionDictionary getEncryptionDictionary()
          This will get the encryption dictionary for this document.
 int getPageCount()
          This will return the total page count of the PDF document.
 PDPage importPage(PDPage page)
          This will import and copy the contents from another location.
 boolean isEncrypted()
          This will tell if this document is encrypted or not.
 boolean isOwnerPassword(String password)
          This will determine if this is the owner password.
 boolean isUserPassword(String password)
          This will determine if this is the user password.
static PDDocument load(File file)
          This will load a document from a file.
static PDDocument load(InputStream input)
          This will load a document from an input stream.
static PDDocument load(String filename)
          This will load a document from a file.
 void save(OutputStream output)
          This will save the document to an output stream.
 void save(String fileName)
          This will save this document to the filesystem.
 void setDocumentInformation(PDDocumentInformation info)
          This will set the document information for this document.
 void setEncryptionDictionary(PDEncryptionDictionary encDictionary)
          This will set the encryption dictionary for this document.
 boolean wasDecryptedWithOwnerPassword()
          This will tell if the document was decrypted with the master password.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PDDocument

public PDDocument()
           throws IOException
Constructor, creates a new PDF Document with no pages. You need to add at least one page for the document to be valid.

Throws:
IOException - If there is an error creating this document.

PDDocument

public PDDocument(COSDocument doc)
Constructor that uses an existing document. The COSDocument that is passed in must be valid.

Parameters:
doc - The COSDocument that this document wraps.
Method Detail

addPage

public void addPage(PDPage page)
This will add a page to the document. This is a convenience method, that will add the page to the root of the hierarchy and set the parent of the page to the root.

Parameters:
page - The page to add to the document.

importPage

public PDPage importPage(PDPage page)
                  throws IOException
This will import and copy the contents from another location. Currently the content stream is stored in a scratch file. The scratch file is associated with the document. If you are adding a page to this document from another document and want to copy the contents to this document's scratch file then use this method otherwise just use the addPage method.

Parameters:
page - The page to import.
Returns:
The page that was imported.
Throws:
IOException - If there is an error copying the page.

getDocument

public COSDocument getDocument()
This will get the low level document.

Returns:
The document that this layer sits on top of.

getDocumentInformation

public PDDocumentInformation getDocumentInformation()
This will get the document info dictionary. This is guaranteed to not return null.

Returns:
The documents /Info dictionary

setDocumentInformation

public void setDocumentInformation(PDDocumentInformation info)
This will set the document information for this document.

Parameters:
info - The updated document information.

getDocumentCatalog

public PDDocumentCatalog getDocumentCatalog()
This will get the document CATALOG. This is guaranteed to not return null.

Returns:
The documents /Root dictionary

isEncrypted

public boolean isEncrypted()
This will tell if this document is encrypted or not.

Returns:
true If this document is encrypted.

getEncryptionDictionary

public PDEncryptionDictionary getEncryptionDictionary()
                                               throws IOException
This will get the encryption dictionary for this document. This will still return the parameters if the document was decrypted. If the document was never encrypted then this will return null. As the encryption architecture in PDF documents is plugable this returns an abstract class, but the only supported subclass at this time is a PDStandardEncryption object.

Returns:
The encryption dictionary(most likely a PDStandardEncryption object)
Throws:
IOException - If there is an error determining which security handler to use.

setEncryptionDictionary

public void setEncryptionDictionary(PDEncryptionDictionary encDictionary)
                             throws IOException
This will set the encryption dictionary for this document.

Parameters:
encDictionary - The encryption dictionary(most likely a PDStandardEncryption object)
Throws:
IOException - If there is an error determining which security handler to use.

isUserPassword

public boolean isUserPassword(String password)
                       throws IOException,
                              CryptographyException
This will determine if this is the user password. This only applies when the document is encrypted and uses standard encryption.

Parameters:
password - The plain text user password.
Returns:
true If the password passed in matches the user password used to encrypt the document.
Throws:
IOException - If there is an error determining if it is the user password.
CryptographyException - If there is an error in the encryption algorithms.

isOwnerPassword

public boolean isOwnerPassword(String password)
                        throws IOException,
                               CryptographyException
This will determine if this is the owner password. This only applies when the document is encrypted and uses standard encryption.

Parameters:
password - The plain text owner password.
Returns:
true If the password passed in matches the owner password used to encrypt the document.
Throws:
IOException - If there is an error determining if it is the user password.
CryptographyException - If there is an error in the encryption algorithms.

decrypt

public void decrypt(String password)
             throws CryptographyException,
                    IOException,
                    InvalidPasswordException
This will decrypt a document.

Parameters:
password - Either the user or owner password.
Throws:
CryptographyException - If there is an error decrypting the document.
IOException - If there is an error getting the stream data.
InvalidPasswordException - If the password is not a user or owner password.

wasDecryptedWithOwnerPassword

public boolean wasDecryptedWithOwnerPassword()
This will tell if the document was decrypted with the master password. This entry is invalid if the PDF was not decrypted.

Returns:
true if the pdf was decrypted with the master password.

encrypt

public void encrypt(String ownerPassword,
                    String userPassword)
             throws CryptographyException,
                    IOException
This will encrypt a document.

Parameters:
ownerPassword - The owner password to encrypt the document.
userPassword - The user password to encrypt the document.
Throws:
CryptographyException - If an error occurs during encryption.
IOException - If there is an error accessing the data.

load

public static PDDocument load(String filename)
                       throws IOException
This will load a document from a file.

Parameters:
filename - The name of the file to load.
Returns:
The document that was loaded.
Throws:
IOException - If there is an error reading from the stream.

load

public static PDDocument load(File file)
                       throws IOException
This will load a document from a file.

Parameters:
file - The name of the file to load.
Returns:
The document that was loaded.
Throws:
IOException - If there is an error reading from the stream.

load

public static PDDocument load(InputStream input)
                       throws IOException
This will load a document from an input stream.

Parameters:
input - The stream that contains the document.
Returns:
The document that was loaded.
Throws:
IOException - If there is an error reading from the stream.

save

public void save(String fileName)
          throws IOException,
                 COSVisitorException
This will save this document to the filesystem.

Parameters:
fileName - The file to save as.
Throws:
IOException - If there is an error saving the document.
COSVisitorException - If an error occurs while generating the data.

save

public void save(OutputStream output)
          throws IOException,
                 COSVisitorException
This will save the document to an output stream.

Parameters:
output - The stream to write to.
Throws:
IOException - If there is an error writing the document.
COSVisitorException - If an error occurs while generating the data.

getPageCount

public int getPageCount()
                 throws IOException
This will return the total page count of the PDF document.

Returns:
The total number of pages in the PDF document.
Throws:
IOException - If there is an error getting the page count

close

public void close()
           throws IOException
This will close the underlying COSDocument object.

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