org.apache.pdfbox.pdmodel
public class PDDocument extends java.lang.Object implements java.awt.print.Pageable
This class implements the Pageable
interface, but since PDFBox
version 1.3.0 you should be using the PDPageable
adapter instead
(see PDFBOX-788).
Constructor and Description |
---|
PDDocument()
Constructor, creates a new PDF Document with no pages.
|
PDDocument(COSDocument doc)
Constructor that uses an existing document.
|
Modifier and Type | Method and Description |
---|---|
void |
addPage(PDPage page)
This will add a page to the document.
|
void |
addSignature(PDSignature sigObject,
SignatureInterface signatureInterface) |
void |
addSignature(PDSignature sigObject,
SignatureInterface signatureInterface,
SignatureOptions options)
This will add a signature to the document.
|
void |
clearWillEncryptWhenSaving()
Deprecated.
Do not rely on this method anymore. It is the responsability of
COSWriter to hold this state.
|
void |
close()
This will close the underlying COSDocument object.
|
void |
decrypt(java.lang.String password)
This will decrypt a document.
|
void |
encrypt(java.lang.String ownerPassword,
java.lang.String userPassword)
This will mark a document to be encrypted.
|
AccessPermission |
getCurrentAccessPermission()
Returns the access permissions granted when the document was decrypted.
|
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 |
getNumberOfPages() |
java.lang.String |
getOwnerPasswordForEncryption()
Deprecated.
Do not rely on this method anymore.
|
int |
getPageCount()
Deprecated.
Use the getNumberOfPages method instead!
|
java.awt.print.PageFormat |
getPageFormat(int pageIndex)
Deprecated.
Use the
PDPageable adapter class |
java.util.Map<java.lang.String,java.lang.Integer> |
getPageMap()
This will return the Map containing the mapping from object-ids to pagenumbers.
|
java.awt.print.Printable |
getPrintable(int pageIndex) |
SecurityHandler |
getSecurityHandler()
Get the security handler that is used for document encryption.
|
PDSignature |
getSignatureDictionary() |
java.lang.String |
getUserPasswordForEncryption()
Deprecated.
Do not rely on this method anymore.
|
PDPage |
importPage(PDPage page)
This will import and copy the contents from another location.
|
boolean |
isAllSecurityToBeRemoved() |
boolean |
isEncrypted()
This will tell if this document is encrypted or not.
|
boolean |
isOwnerPassword(java.lang.String password)
Deprecated.
|
boolean |
isUserPassword(java.lang.String password)
Deprecated.
|
static PDDocument |
load(java.io.File file)
This will load a document from a file.
|
static PDDocument |
load(java.io.File file,
RandomAccess scratchFile)
This will load a document from a file.
|
static PDDocument |
load(java.io.InputStream input)
This will load a document from an input stream.
|
static PDDocument |
load(java.io.InputStream input,
boolean force)
This will load a document from an input stream.
|
static PDDocument |
load(java.io.InputStream input,
RandomAccess scratchFile)
This will load a document from an input stream.
|
static PDDocument |
load(java.io.InputStream input,
RandomAccess scratchFile,
boolean force)
This will load a document from an input stream.
|
static PDDocument |
load(java.lang.String filename)
This will load a document from a file.
|
static PDDocument |
load(java.lang.String filename,
boolean force)
This will load a document from a file.
|
static PDDocument |
load(java.lang.String filename,
RandomAccess scratchFile)
This will load a document from a file.
|
static PDDocument |
load(java.net.URL url)
This will load a document from a url.
|
static PDDocument |
load(java.net.URL url,
boolean force)
This will load a document from a url.
|
static PDDocument |
load(java.net.URL url,
RandomAccess scratchFile)
This will load a document from a url.
|
static PDDocument |
loadNonSeq(java.io.File file,
RandomAccess scratchFile)
Parses PDF with non sequential parser.
|
static PDDocument |
loadNonSeq(java.io.File file,
RandomAccess scratchFile,
java.lang.String password)
Parses PDF with non sequential parser.
|
void |
openProtection(DecryptionMaterial pm)
Tries to decrypt the document in memory using the provided decryption material.
|
void |
print()
This will send the PDF document to a printer.
|
void |
print(java.awt.print.PrinterJob printJob) |
void |
protect(ProtectionPolicy pp)
Protects the document with the protection policy pp.
|
boolean |
removePage(int pageNumber)
Remove the page from the document.
|
boolean |
removePage(PDPage page)
Remove the page from the document.
|
void |
save(java.io.OutputStream output)
This will save the document to an output stream.
|
void |
save(java.lang.String fileName)
This will save this document to the filesystem.
|
void |
saveIncremental(java.io.FileInputStream input,
java.io.OutputStream output) |
void |
saveIncremental(java.lang.String fileName) |
void |
setAllSecurityToBeRemoved(boolean allSecurityToBeRemoved) |
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 |
setSecurityHandler(SecurityHandler _sHandler)
Sets security handler if none is set already.
|
void |
silentPrint()
This will send the PDF to the default printer without prompting the user
for any printer settings.
|
void |
silentPrint(java.awt.print.PrinterJob printJob)
This will send the PDF to the default printer without prompting the user
for any printer settings.
|
boolean |
wasDecryptedWithOwnerPassword()
Deprecated.
use
getCurrentAccessPermission instead |
boolean |
willEncryptWhenSaving()
Deprecated.
Do not rely on this method anymore. It is the responsibility of
COSWriter to hold this state
|
public PDDocument() throws java.io.IOException
java.io.IOException
- If there is an error creating this document.public PDDocument(COSDocument doc)
doc
- The COSDocument that this document wraps.public final java.util.Map<java.lang.String,java.lang.Integer> getPageMap()
public void addPage(PDPage page)
page
- The page to add to the document.public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface) throws java.io.IOException, SignatureException
java.io.IOException
SignatureException
public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface, SignatureOptions options) throws java.io.IOException, SignatureException
sigObject
- is the PDSignature modelsignatureInterface
- is a interface which provides signing capabilitiesoptions
- java.io.IOException
- if there is an error creating required fieldsSignatureException
public boolean removePage(PDPage page)
page
- The page to remove from the document.public boolean removePage(int pageNumber)
pageNumber
- 0 based index to page number.public PDPage importPage(PDPage page) throws java.io.IOException
page
- The page to import.java.io.IOException
- If there is an error copying the page.public COSDocument getDocument()
public PDDocumentInformation getDocumentInformation()
public void setDocumentInformation(PDDocumentInformation info)
info
- The updated document information.public PDDocumentCatalog getDocumentCatalog()
public boolean isEncrypted()
public PDEncryptionDictionary getEncryptionDictionary() throws java.io.IOException
java.io.IOException
- If there is an error determining which security handler to use.public void setEncryptionDictionary(PDEncryptionDictionary encDictionary) throws java.io.IOException
encDictionary
- The encryption dictionary(most likely a PDStandardEncryption object)java.io.IOException
- If there is an error determining which security handler to use.public PDSignature getSignatureDictionary() throws java.io.IOException
java.io.IOException
@Deprecated public boolean isUserPassword(java.lang.String password) throws java.io.IOException, CryptographyException
password
- The plain text user password.java.io.IOException
- If there is an error determining if it is the user password.CryptographyException
- If there is an error in the encryption algorithms.@Deprecated public boolean isOwnerPassword(java.lang.String password) throws java.io.IOException, CryptographyException
password
- The plain text owner password.java.io.IOException
- If there is an error determining if it is the user password.CryptographyException
- If there is an error in the encryption algorithms.public void decrypt(java.lang.String password) throws CryptographyException, java.io.IOException, InvalidPasswordException
password
- Either the user or owner password.CryptographyException
- If there is an error decrypting the document.java.io.IOException
- If there is an error getting the stream data.InvalidPasswordException
- If the password is not a user or owner password.@Deprecated public boolean wasDecryptedWithOwnerPassword()
getCurrentAccessPermission
insteadpublic void encrypt(java.lang.String ownerPassword, java.lang.String userPassword) throws CryptographyException, java.io.IOException
ownerPassword
- The owner password to encrypt the document.userPassword
- The user password to encrypt the document.CryptographyException
- If an error occurs during encryption.java.io.IOException
- If there is an error accessing the data.@Deprecated public java.lang.String getOwnerPasswordForEncryption()
@Deprecated public java.lang.String getUserPasswordForEncryption()
@Deprecated public boolean willEncryptWhenSaving()
@Deprecated public void clearWillEncryptWhenSaving()
public static PDDocument load(java.net.URL url) throws java.io.IOException
url
- The url to load the PDF from.java.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.net.URL url, boolean force) throws java.io.IOException
url
- The url to load the PDF from.force
- When true, the parser will skip corrupt pdf objects and
will continue parsing at the next object in the filejava.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.net.URL url, RandomAccess scratchFile) throws java.io.IOException
url
- The url to load the PDF from.scratchFile
- A location to store temp PDFBox data for this document.java.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.lang.String filename) throws java.io.IOException
filename
- The name of the file to load.java.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.lang.String filename, boolean force) throws java.io.IOException
filename
- The name of the file to load.force
- When true, the parser will skip corrupt pdf objects and
will continue parsing at the next object in the filejava.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.lang.String filename, RandomAccess scratchFile) throws java.io.IOException
filename
- The name of the file to load.scratchFile
- A location to store temp PDFBox data for this document.java.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.io.File file) throws java.io.IOException
file
- The name of the file to load.java.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.io.File file, RandomAccess scratchFile) throws java.io.IOException
file
- The name of the file to load.scratchFile
- A location to store temp PDFBox data for this document.java.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.io.InputStream input) throws java.io.IOException
input
- The stream that contains the document.java.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.io.InputStream input, boolean force) throws java.io.IOException
input
- The stream that contains the document.force
- When true, the parser will skip corrupt pdf objects and
will continue parsing at the next object in the filejava.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.io.InputStream input, RandomAccess scratchFile) throws java.io.IOException
input
- The stream that contains the document.scratchFile
- A location to store temp PDFBox data for this document.java.io.IOException
- If there is an error reading from the stream.public static PDDocument load(java.io.InputStream input, RandomAccess scratchFile, boolean force) throws java.io.IOException
input
- The stream that contains the document.scratchFile
- A location to store temp PDFBox data for this document.force
- When true, the parser will skip corrupt pdf objects and
will continue parsing at the next object in the filejava.io.IOException
- If there is an error reading from the stream.public static PDDocument loadNonSeq(java.io.File file, RandomAccess scratchFile) throws java.io.IOException
file
- file to be loadedscratchFile
- location to store temp PDFBox data for this documentjava.io.IOException
- in case of a file reading or parsing errorpublic static PDDocument loadNonSeq(java.io.File file, RandomAccess scratchFile, java.lang.String password) throws java.io.IOException
file
- file to be loadedscratchFile
- location to store temp PDFBox data for this documentpassword
- password to be used for decryptionjava.io.IOException
- in case of a file reading or parsing errorpublic void save(java.lang.String fileName) throws java.io.IOException, COSVisitorException
fileName
- The file to save as.java.io.IOException
- If there is an error saving the document.COSVisitorException
- If an error occurs while generating the data.public void save(java.io.OutputStream output) throws java.io.IOException, COSVisitorException
output
- The stream to write to.java.io.IOException
- If there is an error writing the document.COSVisitorException
- If an error occurs while generating the data.public void saveIncremental(java.lang.String fileName) throws java.io.IOException, COSVisitorException
java.io.IOException
COSVisitorException
public void saveIncremental(java.io.FileInputStream input, java.io.OutputStream output) throws java.io.IOException, COSVisitorException
java.io.IOException
COSVisitorException
@Deprecated public int getPageCount()
public int getNumberOfPages()
getNumberOfPages
in interface java.awt.print.Pageable
@Deprecated public java.awt.print.PageFormat getPageFormat(int pageIndex)
PDPageable
adapter classPrinterJob.getPrinterJob()
.getPageFormat
in interface java.awt.print.Pageable
i
- page index, zero-basedjava.lang.IndexOutOfBoundsException
- if the page index is invalidpublic java.awt.print.Printable getPrintable(int pageIndex)
getPrintable
in interface java.awt.print.Pageable
public void print(java.awt.print.PrinterJob printJob) throws java.awt.print.PrinterException
printJob
- The printer job.java.awt.print.PrinterException
- If there is an error while sending the PDF to
the printer, or you do not have permissions to print this document.print()
public void print() throws java.awt.print.PrinterException
java.awt.print.PrinterException
- If there is an error while sending the PDF to
the printer, or you do not have permissions to print this document.public void silentPrint() throws java.awt.print.PrinterException
java.awt.print.PrinterException
- If there is an error while printing.print()
public void silentPrint(java.awt.print.PrinterJob printJob) throws java.awt.print.PrinterException
printJob
- A printer job definition.java.awt.print.PrinterException
- If there is an error while printing.print()
public void close() throws java.io.IOException
java.io.IOException
- If there is an error releasing resources.public void protect(ProtectionPolicy pp) throws BadSecurityHandlerException
pp
- The protection policy.BadSecurityHandlerException
- If there is an error during protection.StandardProtectionPolicy
,
PublicKeyProtectionPolicy
public void openProtection(DecryptionMaterial pm) throws BadSecurityHandlerException, java.io.IOException, CryptographyException
pm
- The decryption material (password or certificate).BadSecurityHandlerException
- If there is an error during decryption.java.io.IOException
- If there is an error reading cryptographic information.CryptographyException
- If there is an error during decryption.StandardDecryptionMaterial
,
PublicKeyDecryptionMaterial
public AccessPermission getCurrentAccessPermission()
public SecurityHandler getSecurityHandler()
public boolean setSecurityHandler(SecurityHandler _sHandler)
_sHandler
- security handler to be assigned to documenttrue
if security handler was set, false
otherwise (a security handler was already set)public boolean isAllSecurityToBeRemoved()
public void setAllSecurityToBeRemoved(boolean allSecurityToBeRemoved)