public class PDDocument extends Object implements Pageable, Closeable
This class implements the Pageable
interface, but since PDFBox
version 1.3.0 you should be using the PDPageable
adapter instead
(see PDFBOX-788).
UNKNOWN_NUMBER_OF_PAGES
Constructor and Description |
---|
PDDocument()
Constructor, creates a new PDF Document with no pages.
|
PDDocument(COSDocument doc)
Constructor that uses an existing document.
|
PDDocument(COSDocument doc,
BaseParser usedParser)
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)
Add a signature.
|
void |
addSignature(PDSignature sigObject,
SignatureInterface signatureInterface,
SignatureOptions options)
This will add a signature to the document.
|
void |
addSignatureField(List<PDSignatureField> sigFields,
SignatureInterface signatureInterface,
SignatureOptions options)
This will add a signaturefield 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(String password)
This will decrypt a document.
|
void |
encrypt(String ownerPassword,
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.
|
Long |
getDocumentId() |
PDDocumentInformation |
getDocumentInformation()
This will get the document info dictionary.
|
PDEncryptionDictionary |
getEncryptionDictionary()
This will get the encryption dictionary for this document.
|
PDSignature |
getLastSignatureDictionary()
This will return the last signature.
|
int |
getNumberOfPages() |
String |
getOwnerPasswordForEncryption()
Deprecated.
Do not rely on this method anymore.
|
int |
getPageCount()
Deprecated.
Use the getNumberOfPages method instead!
|
PageFormat |
getPageFormat(int pageIndex)
Deprecated.
Use the
PDPageable adapter class |
Map<String,Integer> |
getPageMap()
This will return the Map containing the mapping from object-ids to pagenumbers.
|
Printable |
getPrintable(int pageIndex) |
SecurityHandler |
getSecurityHandler()
Get the security handler that is used for document encryption.
|
List<PDSignature> |
getSignatureDictionaries()
Retrieve all signature dictionaries from the document.
|
PDSignature |
getSignatureDictionary()
Deprecated.
use
getLastSignatureDictionary() instead. |
List<PDSignatureField> |
getSignatureFields()
Retrieve all signature fields from the document.
|
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()
Indicates if all security is removed or not when writing the pdf.
|
boolean |
isEncrypted()
This will tell if this document is encrypted or not.
|
boolean |
isOwnerPassword(String password)
Deprecated.
|
boolean |
isUserPassword(String password)
Deprecated.
|
static PDDocument |
load(File file)
This will load a document from a file.
|
static PDDocument |
load(File file,
RandomAccess scratchFile)
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(InputStream input,
boolean force)
This will load a document from an input stream.
|
static PDDocument |
load(InputStream input,
RandomAccess scratchFile)
This will load a document from an input stream.
|
static PDDocument |
load(InputStream input,
RandomAccess scratchFile,
boolean force)
This will load a document from an input stream.
|
static PDDocument |
load(String filename)
This will load a document from a file.
|
static PDDocument |
load(String filename,
boolean force)
This will load a document from a file.
|
static PDDocument |
load(String filename,
RandomAccess scratchFile)
This will load a document from a file.
|
static PDDocument |
load(URL url)
This will load a document from a url.
|
static PDDocument |
load(URL url,
boolean force)
This will load a document from a url.
|
static PDDocument |
load(URL url,
RandomAccess scratchFile)
This will load a document from a url.
|
static PDDocument |
loadNonSeq(File file,
RandomAccess scratchFile)
Parses PDF with non sequential parser.
|
static PDDocument |
loadNonSeq(File file,
RandomAccess scratchFile,
String password)
Parses PDF with non sequential parser.
|
static PDDocument |
loadNonSeq(InputStream input,
RandomAccess scratchFile)
Parses PDF with non sequential parser.
|
static PDDocument |
loadNonSeq(InputStream input,
RandomAccess scratchFile,
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(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(File file)
Save the document to a file.
|
void |
save(OutputStream output)
This will save the document to an output stream.
|
void |
save(String fileName)
Save the document to a file.
|
void |
saveIncremental(InputStream input,
OutputStream output)
Save the pdf as incremental.
|
void |
saveIncremental(String fileName)
Save the pdf as incremental.
|
void |
setAllSecurityToBeRemoved(boolean removeAllSecurity)
Activates/Deactivates the removal of all security when writing the pdf.
|
void |
setDocumentId(Long docId) |
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 secHandler)
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(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()
public PDDocument(COSDocument doc)
doc
- The COSDocument that this document wraps.public PDDocument(COSDocument doc, BaseParser usedParser)
doc
- The COSDocument that this document wraps.usedParser
- the parser which is used to read the pdfpublic final Map<String,Integer> getPageMap()
public void addPage(PDPage page)
page
- The page to add to the document.public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface) throws IOException, SignatureException
sigObject
- is the PDSignature modelsignatureInterface
- is a interface which provides signing capabilitiesIOException
- if there is an error creating required fieldsSignatureException
- if something went wrongpublic void addSignature(PDSignature sigObject, SignatureInterface signatureInterface, SignatureOptions options) throws IOException, SignatureException
sigObject
- is the PDSignature modelsignatureInterface
- is a interface which provides signing capabilitiesoptions
- signature optionsIOException
- if there is an error creating required fieldsSignatureException
- if something went wrongpublic void addSignatureField(List<PDSignatureField> sigFields, SignatureInterface signatureInterface, SignatureOptions options) throws IOException, SignatureException
sigFields
- are the PDSignatureFields that should be added to the documentsignatureInterface
- is a interface which provides signing capabilitiesoptions
- signature optionsIOException
- 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 IOException
page
- The page to import.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 IOException
IOException
- If there is an error determining which security handler to use.public void setEncryptionDictionary(PDEncryptionDictionary encDictionary) throws IOException
encDictionary
- The encryption dictionary(most likely a PDStandardEncryption object)IOException
- If there is an error determining which security handler to use.@Deprecated public PDSignature getSignatureDictionary() throws IOException
getLastSignatureDictionary()
instead.PDSignature
.IOException
- if no document catalog can be found.public PDSignature getLastSignatureDictionary() throws IOException
PDSignature
.IOException
- if no document catalog can be found.public List<PDSignatureField> getSignatureFields() throws IOException
List
of PDSignatureField
sIOException
- if no document catalog can be found.public List<PDSignature> getSignatureDictionaries() throws IOException
List
of PDSignature
sIOException
- if no document catalog can be found.@Deprecated public boolean isUserPassword(String password) throws IOException, CryptographyException
password
- The plain text user password.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(String password) throws IOException, CryptographyException
password
- The plain text owner password.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(String password) throws CryptographyException, IOException
password
- Either the user or owner password.CryptographyException
- If there is an error decrypting the document.IOException
- If there is an error getting the stream data.@Deprecated public boolean wasDecryptedWithOwnerPassword()
getCurrentAccessPermission
insteadpublic void encrypt(String ownerPassword, String userPassword) throws CryptographyException, IOException
ownerPassword
- The owner password to encrypt the document.userPassword
- The user password to encrypt the document.CryptographyException
- If an error occurs during encryption.IOException
- If there is an error accessing the data.@Deprecated public String getOwnerPasswordForEncryption()
@Deprecated public String getUserPasswordForEncryption()
@Deprecated public boolean willEncryptWhenSaving()
@Deprecated public void clearWillEncryptWhenSaving()
public static PDDocument load(URL url) throws IOException
url
- The url to load the PDF from.IOException
- If there is an error reading from the stream.public static PDDocument load(URL url, boolean force) throws 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 fileIOException
- If there is an error reading from the stream.public static PDDocument load(URL url, RandomAccess scratchFile) throws IOException
url
- The url to load the PDF from.scratchFile
- A location to store temp PDFBox data for this document.IOException
- If there is an error reading from the stream.public static PDDocument load(String filename) throws IOException
filename
- The name of the file to load.IOException
- If there is an error reading from the stream.public static PDDocument load(String filename, boolean force) throws 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 fileIOException
- If there is an error reading from the stream.public static PDDocument load(String filename, RandomAccess scratchFile) throws IOException
filename
- The name of the file to load.scratchFile
- A location to store temp PDFBox data for this document.IOException
- If there is an error reading from the stream.public static PDDocument load(File file) throws IOException
file
- The name of the file to load.IOException
- If there is an error reading from the stream.public static PDDocument load(File file, RandomAccess scratchFile) throws IOException
file
- The name of the file to load.scratchFile
- A location to store temp PDFBox data for this document.IOException
- If there is an error reading from the stream.public static PDDocument load(InputStream input) throws IOException
input
- The stream that contains the document.IOException
- If there is an error reading from the stream.public static PDDocument load(InputStream input, boolean force) throws 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 fileIOException
- If there is an error reading from the stream.public static PDDocument load(InputStream input, RandomAccess scratchFile) throws IOException
input
- The stream that contains the document.scratchFile
- A location to store temp PDFBox data for this document.IOException
- If there is an error reading from the stream.public static PDDocument load(InputStream input, RandomAccess scratchFile, boolean force) throws 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 fileIOException
- If there is an error reading from the stream.public static PDDocument loadNonSeq(File file, RandomAccess scratchFile) throws IOException
file
- file to be loadedscratchFile
- location to store temp PDFBox data for this documentIOException
- in case of a file reading or parsing errorpublic static PDDocument loadNonSeq(File file, RandomAccess scratchFile, String password) throws IOException
file
- file to be loadedscratchFile
- location to store temp PDFBox data for this documentpassword
- password to be used for decryptionIOException
- in case of a file reading or parsing errorpublic static PDDocument loadNonSeq(InputStream input, RandomAccess scratchFile) throws IOException
input
- stream that contains the document.scratchFile
- location to store temp PDFBox data for this documentIOException
- in case of a file reading or parsing errorpublic static PDDocument loadNonSeq(InputStream input, RandomAccess scratchFile, String password) throws IOException
input
- stream that contains the document.scratchFile
- location to store temp PDFBox data for this documentpassword
- password to be used for decryptionIOException
- in case of a file reading or parsing errorpublic void save(String fileName) throws IOException, COSVisitorException
fileName
- The file to save as.IOException
- If there is an error saving the document.COSVisitorException
- If an error occurs while generating the data.public void save(File file) throws IOException, COSVisitorException
file
- The file to save as.IOException
- If there is an error saving the document.COSVisitorException
- If an error occurs while generating the data.public void save(OutputStream output) throws IOException, COSVisitorException
output
- The stream to write to.IOException
- If there is an error writing the document.COSVisitorException
- If an error occurs while generating the data.public void saveIncremental(String fileName) throws IOException, COSVisitorException
fileName
- the filename to be usedIOException
- if something went wrongCOSVisitorException
- if something went wrongpublic void saveIncremental(InputStream input, OutputStream output) throws IOException, COSVisitorException
input
- output
- IOException
- if something went wrongCOSVisitorException
- if something went wrong@Deprecated public int getPageCount()
public int getNumberOfPages()
getNumberOfPages
in interface Pageable
@Deprecated public PageFormat getPageFormat(int pageIndex)
PDPageable
adapter classPrinterJob.getPrinterJob()
.getPageFormat
in interface Pageable
pageIndex
- page index, zero-basedpublic Printable getPrintable(int pageIndex)
getPrintable
in interface Pageable
public void print(PrinterJob printJob) throws PrinterException
printJob
- The printer job.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 PrinterException
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 PrinterException
PrinterException
- If there is an error while printing.print()
public void silentPrint(PrinterJob printJob) throws PrinterException
printJob
- A printer job definition.PrinterException
- If there is an error while printing.print()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
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, IOException, CryptographyException
pm
- The decryption material (password or certificate).BadSecurityHandlerException
- If there is an error during decryption.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 secHandler)
secHandler
- 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 removeAllSecurity)
removeAllSecurity
- remove all security if set to truepublic Long getDocumentId()
public void setDocumentId(Long docId)
Copyright © 2002-2015 The Apache Software Foundation. All Rights Reserved.