public class PDDocument extends Object implements Closeable
Constructor and Description |
---|
PDDocument()
Creates an empty PDF document.
|
PDDocument(COSDocument doc)
Constructor that uses an existing document.
|
PDDocument(COSDocument doc,
RandomAccessRead source)
Constructor that uses an existing document.
|
PDDocument(COSDocument doc,
RandomAccessRead source,
AccessPermission permission)
Constructor that uses an existing document.
|
PDDocument(MemoryUsageSetting memUsageSetting)
Creates an empty PDF document.
|
Modifier and Type | Method and Description |
---|---|
void |
addPage(PDPage page)
This will add a page to the document.
|
void |
addSignature(PDSignature sigObject)
Add parameters of signature to be created externally using default signature options.
|
void |
addSignature(PDSignature sigObject,
SignatureInterface signatureInterface)
Add a signature to be created using the instance of given interface.
|
void |
addSignature(PDSignature sigObject,
SignatureInterface signatureInterface,
SignatureOptions options)
This will add a signature to the document.
|
void |
addSignature(PDSignature sigObject,
SignatureOptions options)
Add parameters of signature to be created externally.
|
void |
addSignatureField(List<PDSignatureField> sigFields,
SignatureInterface signatureInterface,
SignatureOptions options)
This will add a list of signature fields to the document.
|
void |
close()
This will close the underlying COSDocument object.
|
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()
Provides the document ID.
|
PDDocumentInformation |
getDocumentInformation()
This will get the document info dictionary.
|
PDEncryption |
getEncryption()
This will get the encryption dictionary for this document.
|
PDSignature |
getLastSignatureDictionary()
This will return the last signature.
|
int |
getNumberOfPages()
This will return the total page count of the PDF document.
|
PDPage |
getPage(int pageIndex)
Returns the page at the given index.
|
PDPageTree |
getPages()
Returns the page tree.
|
ResourceCache |
getResourceCache()
Returns the resource cache associated with this document, or null if there is none.
|
List<PDSignature> |
getSignatureDictionaries()
Retrieve all signature dictionaries from the document.
|
List<PDSignatureField> |
getSignatureFields()
Retrieve all signature fields from the document.
|
float |
getVersion()
Returns the PDF specification version this document conforms to.
|
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.
|
static PDDocument |
load(byte[] input)
Parses a PDF.
|
static PDDocument |
load(byte[] input,
String password)
Parses a PDF.
|
static PDDocument |
load(byte[] input,
String password,
InputStream keyStore,
String alias)
Parses a PDF.
|
static PDDocument |
load(byte[] input,
String password,
InputStream keyStore,
String alias,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(File file)
Parses a PDF.
|
static PDDocument |
load(File file,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(File file,
String password)
Parses a PDF.
|
static PDDocument |
load(File file,
String password,
InputStream keyStore,
String alias)
Parses a PDF.
|
static PDDocument |
load(File file,
String password,
InputStream keyStore,
String alias,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(File file,
String password,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(InputStream input)
Parses a PDF.
|
static PDDocument |
load(InputStream input,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(InputStream input,
String password)
Parses a PDF.
|
static PDDocument |
load(InputStream input,
String password,
InputStream keyStore,
String alias)
Parses a PDF.
|
static PDDocument |
load(InputStream input,
String password,
InputStream keyStore,
String alias,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(InputStream input,
String password,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
void |
protect(ProtectionPolicy policy)
Protects the document with a protection policy.
|
void |
removePage(int pageNumber)
Remove the page from the document.
|
void |
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(OutputStream output)
Save the PDF as an incremental update.
|
ExternalSigningSupport |
saveIncrementalForExternalSigning(OutputStream output)
(This is a new feature for 2.0.3.
|
void |
setAllSecurityToBeRemoved(boolean removeAllSecurity)
Activates/Deactivates the removal of all security when writing the pdf.
|
void |
setDocumentId(Long docId)
Sets the document ID to the given value.
|
void |
setDocumentInformation(PDDocumentInformation info)
This will set the document information for this document.
|
void |
setEncryptionDictionary(PDEncryption encryption)
This will set the encryption dictionary for this document.
|
void |
setResourceCache(ResourceCache resourceCache)
Sets the resource cache associated with this document.
|
void |
setVersion(float newVersion)
Sets the PDF specification version for this document.
|
public PDDocument()
public PDDocument(MemoryUsageSetting memUsageSetting)
memUsageSetting
- defines how memory is used for buffering PDF streamspublic PDDocument(COSDocument doc)
doc
- The COSDocument that this document wraps.public PDDocument(COSDocument doc, RandomAccessRead source)
doc
- The COSDocument that this document wraps.source
- the parser which is used to read the pdfpublic PDDocument(COSDocument doc, RandomAccessRead source, AccessPermission permission)
doc
- The COSDocument that this document wraps.source
- the parser which is used to read the pdfpermission
- he access permissions of the pdfpublic void addPage(PDPage page)
page
- The page to add to the document.public void addSignature(PDSignature sigObject) throws IOException
saveIncrementalForExternalSigning(OutputStream)
method description on external
signature creation scenario details.sigObject
- is the PDSignatureField modelIOException
- if there is an error creating required fieldspublic void addSignature(PDSignature sigObject, SignatureOptions options) throws IOException
saveIncrementalForExternalSigning(OutputStream)
method description on external
signature creation scenario details.sigObject
- is the PDSignatureField modeloptions
- signature optionsIOException
- if there is an error creating required fieldspublic void addSignature(PDSignature sigObject, SignatureInterface signatureInterface) throws IOException
sigObject
- is the PDSignatureField modelsignatureInterface
- is an interface which provides signing capabilitiesIOException
- if there is an error creating required fieldspublic void addSignature(PDSignature sigObject, SignatureInterface signatureInterface, SignatureOptions options) throws IOException
sigObject
- is the PDSignatureField modelsignatureInterface
- is an interface which provides signing capabilitiesoptions
- signature optionsIOException
- if there is an error creating required fieldspublic void addSignatureField(List<PDSignatureField> sigFields, SignatureInterface signatureInterface, SignatureOptions options) throws IOException
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 fieldspublic void removePage(PDPage page)
page
- The page to remove from the document.public void removePage(int pageNumber)
pageNumber
- 0 based index to page number.public PDPage importPage(PDPage page) throws IOException
addPage
method.
Unlike addPage
, this method creates a new PDPage object. If your page has
annotations, and if these link to pages not in the target document, then the target document
might become huge. What you need to do is to delete page references of such annotations. See
here for how to do this.
Inherited (global) resources are ignored. If you need them, call
importedPage.setRotation(page.getRotation());
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 PDEncryption getEncryption()
public void setEncryptionDictionary(PDEncryption encryption) throws IOException
encryption
- The encryption dictionary(most likely a PDStandardEncryption object)IOException
- If there is an error determining which security handler to use.public PDSignature getLastSignatureDictionary() throws IOException
PDSignatureField
.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 PDSignatureField
sIOException
- if no document catalog can be found.public static PDDocument load(File file) throws InvalidPasswordException, IOException
file
- file to be loadedInvalidPasswordException
- If the file required a non-empty password.IOException
- in case of a file reading or parsing errorpublic static PDDocument load(File file, MemoryUsageSetting memUsageSetting) throws InvalidPasswordException, IOException
file
- file to be loadedmemUsageSetting
- defines how memory is used for buffering PDF streamsInvalidPasswordException
- If the file required a non-empty password.IOException
- in case of a file reading or parsing errorpublic static PDDocument load(File file, String password) throws InvalidPasswordException, IOException
file
- file to be loadedpassword
- password to be used for decryptionInvalidPasswordException
- If the password is incorrect.IOException
- in case of a file reading or parsing errorpublic static PDDocument load(File file, String password, MemoryUsageSetting memUsageSetting) throws InvalidPasswordException, IOException
file
- file to be loadedpassword
- password to be used for decryptionmemUsageSetting
- defines how memory is used for buffering PDF streamsInvalidPasswordException
- If the password is incorrect.IOException
- in case of a file reading or parsing errorpublic static PDDocument load(File file, String password, InputStream keyStore, String alias) throws IOException
file
- file to be loadedpassword
- password to be used for decryptionkeyStore
- key store to be used for decryption when using public key securityalias
- alias to be used for decryption when using public key securityIOException
- in case of a file reading or parsing errorpublic static PDDocument load(File file, String password, InputStream keyStore, String alias, MemoryUsageSetting memUsageSetting) throws IOException
file
- file to be loadedpassword
- password to be used for decryptionkeyStore
- key store to be used for decryption when using public key securityalias
- alias to be used for decryption when using public key securitymemUsageSetting
- defines how memory is used for buffering PDF streamsIOException
- in case of a file reading or parsing errorpublic static PDDocument load(InputStream input) throws InvalidPasswordException, IOException
input
- stream that contains the document.InvalidPasswordException
- If the PDF required a non-empty password.IOException
- In case of a reading or parsing error.public static PDDocument load(InputStream input, MemoryUsageSetting memUsageSetting) throws InvalidPasswordException, IOException
input
- stream that contains the document.memUsageSetting
- defines how memory is used for buffering input stream and PDF streamsInvalidPasswordException
- If the PDF required a non-empty password.IOException
- In case of a reading or parsing error.public static PDDocument load(InputStream input, String password) throws InvalidPasswordException, IOException
input
- stream that contains the document.password
- password to be used for decryptionInvalidPasswordException
- If the password is incorrect.IOException
- In case of a reading or parsing error.public static PDDocument load(InputStream input, String password, InputStream keyStore, String alias) throws IOException
input
- stream that contains the document.password
- password to be used for decryptionkeyStore
- key store to be used for decryption when using public key securityalias
- alias to be used for decryption when using public key securityIOException
- In case of a reading or parsing error.public static PDDocument load(InputStream input, String password, MemoryUsageSetting memUsageSetting) throws InvalidPasswordException, IOException
input
- stream that contains the document.password
- password to be used for decryptionmemUsageSetting
- defines how memory is used for buffering input stream and PDF streamsInvalidPasswordException
- If the password is incorrect.IOException
- In case of a reading or parsing error.public static PDDocument load(InputStream input, String password, InputStream keyStore, String alias, MemoryUsageSetting memUsageSetting) throws IOException
input
- stream that contains the document.password
- password to be used for decryptionkeyStore
- key store to be used for decryption when using public key securityalias
- alias to be used for decryption when using public key securitymemUsageSetting
- defines how memory is used for buffering input stream and PDF streamsInvalidPasswordException
- If the password is incorrect.IOException
- In case of a reading or parsing error.public static PDDocument load(byte[] input) throws InvalidPasswordException, IOException
input
- byte array that contains the document.InvalidPasswordException
- If the PDF required a non-empty password.IOException
- In case of a reading or parsing error.public static PDDocument load(byte[] input, String password) throws InvalidPasswordException, IOException
input
- byte array that contains the document.password
- password to be used for decryptionInvalidPasswordException
- If the password is incorrect.IOException
- In case of a reading or parsing error.public static PDDocument load(byte[] input, String password, InputStream keyStore, String alias) throws IOException
input
- byte array that contains the document.password
- password to be used for decryptionkeyStore
- key store to be used for decryption when using public key securityalias
- alias to be used for decryption when using public key securityInvalidPasswordException
- If the password is incorrect.IOException
- In case of a reading or parsing error.public static PDDocument load(byte[] input, String password, InputStream keyStore, String alias, MemoryUsageSetting memUsageSetting) throws IOException
input
- byte array that contains the document.password
- password to be used for decryptionkeyStore
- key store to be used for decryption when using public key securityalias
- alias to be used for decryption when using public key securitymemUsageSetting
- defines how memory is used for buffering input stream and PDF streamsInvalidPasswordException
- If the password is incorrect.IOException
- In case of a reading or parsing error.public void save(String fileName) throws IOException
fileName
- The file to save as.IOException
- if the output could not be writtenpublic void save(File file) throws IOException
file
- The file to save as.IOException
- if the output could not be writtenpublic void save(OutputStream output) throws IOException
output
- The stream to write to. It is recommended to wrap it in a
BufferedOutputStream
, unless it is already buffered.IOException
- if the output could not be writtenpublic void saveIncremental(OutputStream output) throws IOException
output
- stream to writeIOException
- if the output could not be writtenIllegalStateException
- if the document was not loaded from a file or a stream.public ExternalSigningSupport saveIncrementalForExternalSigning(OutputStream output) throws IOException
(This is a new feature for 2.0.3. The API for external signing might change based on feedback after release!)
Save PDF incrementally without closing for external signature creation scenario. The general sequence is:
PDDocument pdDocument = ...; OutputStream outputStream = ...; SignatureOptions signatureOptions = ...; // options to specify fine tuned signature options or null for defaults PDSignature pdSignature = ...; // add signature parameters to be used when creating signature dictionary pdDocument.addSignature(pdSignature, signatureOptions); // prepare PDF for signing and obtain helper class to be used ExternalSigningSupport externalSigningSupport = pdDocument.saveIncrementalForExternalSigning(outputStream); // get data to be signed InputStream dataToBeSigned = externalSigningSupport.getContent(); // invoke signature service byte[] signature = sign(dataToBeSigned); // set resulted CMS signature externalSigningSupport.setSignature(signature); // last step is to close the document pdDocument.close();
Note that after calling this method, only close()
method may invoked for
PDDocument
instance and only AFTER ExternalSigningSupport
instance is used.
output
- stream to write final PDFIOException
- if the output could not be writtenIllegalStateException
- if the document was not loaded from a file or a stream or
signature optionss were not set.public PDPage getPage(int pageIndex)
pageIndex
- the page indexpublic PDPageTree getPages()
public int getNumberOfPages()
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 policy) throws IOException
setAllSecurityToBeRemoved(boolean)
with a false argument if it was set to true
previously and logs a warning.policy
- The protection policy.IOException
- if there isn't any suitable security handler.StandardProtectionPolicy
,
PublicKeyProtectionPolicy
public AccessPermission getCurrentAccessPermission()
public boolean isAllSecurityToBeRemoved()
public void setAllSecurityToBeRemoved(boolean removeAllSecurity)
removeAllSecurity
- remove all security if set to truepublic Long getDocumentId()
public void setDocumentId(Long docId)
docId
- the new document IDpublic float getVersion()
public void setVersion(float newVersion)
newVersion
- the new PDF version (e.g. 1.4f)public ResourceCache getResourceCache()
public void setResourceCache(ResourceCache resourceCache)
resourceCache
- A resource cache, or null.Copyright © 2002–2017 The Apache Software Foundation. All rights reserved.