public class PDDocument
extends java.lang.Object
implements java.io.Closeable
Modifier and Type | Field and Description |
---|---|
private AccessPermission |
accessPermission |
private boolean |
allSecurityToBeRemoved |
private COSDocument |
document |
private PDDocumentCatalog |
documentCatalog |
private java.lang.Long |
documentId |
private PDDocumentInformation |
documentInformation |
private PDEncryption |
encryption |
private java.util.Set<TrueTypeFont> |
fontsToClose |
private java.util.Set<PDFont> |
fontsToSubset |
private static org.apache.commons.logging.Log |
LOG |
private RandomAccessRead |
pdfSource |
private static int[] |
RESERVE_BYTE_RANGE
For signing: large reserve byte range used as placeholder in the saved PDF until the actual
length of the PDF is known.
|
private ResourceCache |
resourceCache |
private boolean |
signatureAdded |
private SigningSupport |
signingSupport |
private SignatureInterface |
signInterface |
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(java.util.List<PDSignatureField> sigFields,
SignatureInterface signatureInterface,
SignatureOptions options)
Deprecated.
The method is misleading, because only one signature may be
added in a document. The method will be removed in the future.
|
private void |
assignAcroFormDefaultResource(PDAcroForm acroForm,
COSDictionary newDict) |
private void |
assignAppearanceDictionary(PDSignatureField signatureField,
COSDictionary apDict) |
private void |
assignSignatureRectangle(PDSignatureField signatureField,
COSDictionary annotDict) |
private boolean |
checkSignatureAnnotation(java.util.List<PDAnnotation> annotations,
PDAnnotationWidget widget)
Check if the widget already exists in the annotation list
|
private boolean |
checkSignatureField(java.util.Iterator<PDField> fieldIterator,
PDSignatureField signatureField)
Check if the field already exists in the field list.
|
void |
close()
This will close the underlying COSDocument object.
|
private PDSignatureField |
findSignatureField(java.util.Iterator<PDField> fieldIterator,
PDSignature sigObject)
Search acroform fields for signature field with specific signature dictionary.
|
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.
|
java.lang.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.
|
(package private) java.util.Set<PDFont> |
getFontsToSubset()
Returns the list of fonts which will be subset before the document is saved.
|
PDSignature |
getLastSignatureDictionary()
This will return the last signature from the field tree.
|
int |
getNumberOfPages()
This will return the total page count of the PDF document.
|
PDPage |
getPage(int pageIndex)
Returns the page at the given 0-based index.
|
PDPageTree |
getPages()
Returns the page tree.
|
ResourceCache |
getResourceCache()
Returns the resource cache associated with this document, or null if there is none.
|
java.util.List<PDSignature> |
getSignatureDictionaries()
Retrieve all signature dictionaries from the document.
|
java.util.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,
java.lang.String password)
Parses a PDF.
|
static PDDocument |
load(byte[] input,
java.lang.String password,
java.io.InputStream keyStore,
java.lang.String alias)
Parses a PDF.
|
static PDDocument |
load(byte[] input,
java.lang.String password,
java.io.InputStream keyStore,
java.lang.String alias,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(java.io.File file)
Parses a PDF.
|
static PDDocument |
load(java.io.File file,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(java.io.File file,
java.lang.String password)
Parses a PDF.
|
static PDDocument |
load(java.io.File file,
java.lang.String password,
java.io.InputStream keyStore,
java.lang.String alias)
Parses a PDF.
|
static PDDocument |
load(java.io.File file,
java.lang.String password,
java.io.InputStream keyStore,
java.lang.String alias,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(java.io.File file,
java.lang.String password,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(java.io.InputStream input)
Parses a PDF.
|
static PDDocument |
load(java.io.InputStream input,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(java.io.InputStream input,
java.lang.String password)
Parses a PDF.
|
static PDDocument |
load(java.io.InputStream input,
java.lang.String password,
java.io.InputStream keyStore,
java.lang.String alias)
Parses a PDF.
|
static PDDocument |
load(java.io.InputStream input,
java.lang.String password,
java.io.InputStream keyStore,
java.lang.String alias,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
static PDDocument |
load(java.io.InputStream input,
java.lang.String password,
MemoryUsageSetting memUsageSetting)
Parses a PDF.
|
private static PDDocument |
load(RandomAccessBufferedFileInputStream raFile,
java.lang.String password,
java.io.InputStream keyStore,
java.lang.String alias,
MemoryUsageSetting memUsageSetting) |
private void |
prepareNonVisibleSignature(PDSignatureField signatureField) |
private void |
prepareVisibleSignature(PDSignatureField signatureField,
PDAcroForm acroForm,
COSDocument visualSignature) |
void |
protect(ProtectionPolicy policy)
Protects the document with a protection policy.
|
void |
registerTrueTypeFontForClosing(TrueTypeFont ttf)
For internal PDFBox use when creating PDF documents: register a TrueTypeFont to make sure it
is closed when the PDDocument is closed to avoid memory leaks.
|
void |
removePage(int pageNumber)
Remove the page from the document.
|
void |
removePage(PDPage page)
Remove the page from the document.
|
void |
save(java.io.File file)
Save the document to a file.
|
void |
save(java.io.OutputStream output)
This will save the document to an output stream.
|
void |
save(java.lang.String fileName)
Save the document to a file.
|
void |
saveIncremental(java.io.OutputStream output)
Save the PDF as an incremental update.
|
ExternalSigningSupport |
saveIncrementalForExternalSigning(java.io.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(java.lang.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.
|
private static final int[] RESERVE_BYTE_RANGE
PDSignature.getByteRange()
) and reassign this yourself (with
PDSignature.setByteRange(int[])
) only if you call
saveIncrementalForExternalSigning()
twice.private static final org.apache.commons.logging.Log LOG
private final COSDocument document
private PDDocumentInformation documentInformation
private PDDocumentCatalog documentCatalog
private PDEncryption encryption
private boolean allSecurityToBeRemoved
private java.lang.Long documentId
private final RandomAccessRead pdfSource
private AccessPermission accessPermission
private final java.util.Set<PDFont> fontsToSubset
private final java.util.Set<TrueTypeFont> fontsToClose
private SignatureInterface signInterface
private SigningSupport signingSupport
private ResourceCache resourceCache
private boolean signatureAdded
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 java.io.IOException
saveIncrementalForExternalSigning(OutputStream)
method description on external
signature creation scenario details.
Only one signature may be added in a document. To sign several times, load document, add signature, save incremental and close again.
sigObject
- is the PDSignatureField modeljava.io.IOException
- if there is an error creating required fieldsjava.lang.IllegalStateException
- if one attempts to add several signature
fields.public void addSignature(PDSignature sigObject, SignatureOptions options) throws java.io.IOException
saveIncrementalForExternalSigning(OutputStream)
method description on external
signature creation scenario details.
Only one signature may be added in a document. To sign several times, load document, add signature, save incremental and close again.
sigObject
- is the PDSignatureField modeloptions
- signature optionsjava.io.IOException
- if there is an error creating required fieldsjava.lang.IllegalStateException
- if one attempts to add several signature
fields.public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface) throws java.io.IOException
Only one signature may be added in a document. To sign several times, load document, add signature, save incremental and close again.
sigObject
- is the PDSignatureField modelsignatureInterface
- is an interface whose implementation provides
signing capabilities. Can be null if external signing if used.java.io.IOException
- if there is an error creating required fieldsjava.lang.IllegalStateException
- if one attempts to add several signature
fields.public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface, SignatureOptions options) throws java.io.IOException
Only one signature may be added in a document. To sign several times, load document, add signature, save incremental and close again.
sigObject
- is the PDSignatureField modelsignatureInterface
- is an interface whose implementation provides
signing capabilities. Can be null if external signing if used.options
- signature optionsjava.io.IOException
- if there is an error creating required fieldsjava.lang.IllegalStateException
- if one attempts to add several signature
fields.private PDSignatureField findSignatureField(java.util.Iterator<PDField> fieldIterator, PDSignature sigObject)
fieldIterator
- iterator on all fields.sigObject
- signature object (the /V part).private boolean checkSignatureField(java.util.Iterator<PDField> fieldIterator, PDSignatureField signatureField)
fieldIterator
- iterator on all fields.signatureField
- the signature field.private boolean checkSignatureAnnotation(java.util.List<PDAnnotation> annotations, PDAnnotationWidget widget)
acroFormFields
- the list of AcroForm fields.signatureField
- the signature field.private void prepareVisibleSignature(PDSignatureField signatureField, PDAcroForm acroForm, COSDocument visualSignature)
private void assignSignatureRectangle(PDSignatureField signatureField, COSDictionary annotDict)
private void assignAppearanceDictionary(PDSignatureField signatureField, COSDictionary apDict)
private void assignAcroFormDefaultResource(PDAcroForm acroForm, COSDictionary newDict)
private void prepareNonVisibleSignature(PDSignatureField signatureField)
@Deprecated public void addSignatureField(java.util.List<PDSignatureField> sigFields, SignatureInterface signatureInterface, SignatureOptions options) throws java.io.IOException
sigFields
- are the PDSignatureFields that should be added to the documentsignatureInterface
- is an interface whose implementation provides
signing capabilities. Can be null if external signing if used.options
- signature optionsjava.io.IOException
- 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 java.io.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 because these can contain resources not needed for
this page which could bloat your document, see
PDFBOX-28 and related issues.
If you need them, call importedPage.setResources(page.getResources());
This method should only be used to import a page from a loaded document, not from a generated document because these can contain unfinished parts, e.g. font subsetting information.
page
- The page to import.java.io.IOException
- If there is an error copying the page.public COSDocument getDocument()
public PDDocumentInformation getDocumentInformation()
In PDF 2.0 this is deprecated except for two entries, /CreationDate and /ModDate. For any other
document level metadata, a metadata stream should be used instead, see
PDDocumentCatalog.getMetadata()
.
public void setDocumentInformation(PDDocumentInformation info)
In PDF 2.0 this is deprecated except for two entries, /CreationDate and /ModDate. For any other
document level metadata, a metadata stream should be used instead, see
PDDocumentCatalog#setMetadata(PDMetadata)
.
info
- The updated document information.public PDDocumentCatalog getDocumentCatalog()
public boolean isEncrypted()
public PDEncryption getEncryption()
public void setEncryptionDictionary(PDEncryption encryption) throws java.io.IOException
encryption
- The encryption dictionary(most likely a PDStandardEncryption object)java.io.IOException
- If there is an error determining which security handler to use.public PDSignature getLastSignatureDictionary() throws java.io.IOException
PDSignatureField
.java.io.IOException
- if no document catalog can be found.public java.util.List<PDSignatureField> getSignatureFields() throws java.io.IOException
List
of PDSignatureField
sjava.io.IOException
- if no document catalog can be found.public java.util.List<PDSignature> getSignatureDictionaries() throws java.io.IOException
List
of PDSignatureField
sjava.io.IOException
- if no document catalog can be found.public void registerTrueTypeFontForClosing(TrueTypeFont ttf)
ttf
- java.util.Set<PDFont> getFontsToSubset()
public static PDDocument load(java.io.File file) throws java.io.IOException
file
- file to be loadedInvalidPasswordException
- If the file required a non-empty password.java.io.IOException
- in case of a file reading or parsing errorpublic static PDDocument load(java.io.File file, MemoryUsageSetting memUsageSetting) throws java.io.IOException
file
- file to be loadedmemUsageSetting
- defines how memory is used for buffering PDF streamsInvalidPasswordException
- If the file required a non-empty password.java.io.IOException
- in case of a file reading or parsing errorpublic static PDDocument load(java.io.File file, java.lang.String password) throws java.io.IOException
file
- file to be loadedpassword
- password to be used for decryptionInvalidPasswordException
- If the password is incorrect.java.io.IOException
- in case of a file reading or parsing errorpublic static PDDocument load(java.io.File file, java.lang.String password, MemoryUsageSetting memUsageSetting) throws java.io.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.java.io.IOException
- in case of a file reading or parsing errorpublic static PDDocument load(java.io.File file, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias) throws java.io.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 securityjava.io.IOException
- in case of a file reading or parsing errorpublic static PDDocument load(java.io.File file, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias, MemoryUsageSetting memUsageSetting) throws java.io.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 streamsjava.io.IOException
- in case of a file reading or parsing errorprivate static PDDocument load(RandomAccessBufferedFileInputStream raFile, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias, MemoryUsageSetting memUsageSetting) throws java.io.IOException
java.io.IOException
public static PDDocument load(java.io.InputStream input) throws java.io.IOException
input
- stream that contains the document. Don't forget to close it after loading.InvalidPasswordException
- If the PDF required a non-empty password.java.io.IOException
- In case of a reading or parsing error.public static PDDocument load(java.io.InputStream input, MemoryUsageSetting memUsageSetting) throws java.io.IOException
input
- stream that contains the document. Don't forget to close it after loading.memUsageSetting
- defines how memory is used for buffering input stream and PDF streamsInvalidPasswordException
- If the PDF required a non-empty password.java.io.IOException
- In case of a reading or parsing error.public static PDDocument load(java.io.InputStream input, java.lang.String password) throws java.io.IOException
input
- stream that contains the document. Don't forget to close it after loading.password
- password to be used for decryptionInvalidPasswordException
- If the password is incorrect.java.io.IOException
- In case of a reading or parsing error.public static PDDocument load(java.io.InputStream input, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias) throws java.io.IOException
input
- stream that contains the document. Don't forget to close it after loading.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 securityjava.io.IOException
- In case of a reading or parsing error.public static PDDocument load(java.io.InputStream input, java.lang.String password, MemoryUsageSetting memUsageSetting) throws java.io.IOException
input
- stream that contains the document. Don't forget to close it after loading.password
- password to be used for decryptionmemUsageSetting
- defines how memory is used for buffering input stream and PDF streamsInvalidPasswordException
- If the password is incorrect.java.io.IOException
- In case of a reading or parsing error.public static PDDocument load(java.io.InputStream input, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias, MemoryUsageSetting memUsageSetting) throws java.io.IOException
input
- stream that contains the document. Don't forget to close it after loading.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.java.io.IOException
- In case of a reading or parsing error.public static PDDocument load(byte[] input) throws java.io.IOException
input
- byte array that contains the document.InvalidPasswordException
- If the PDF required a non-empty password.java.io.IOException
- In case of a reading or parsing error.public static PDDocument load(byte[] input, java.lang.String password) throws java.io.IOException
input
- byte array that contains the document.password
- password to be used for decryptionInvalidPasswordException
- If the password is incorrect.java.io.IOException
- In case of a reading or parsing error.public static PDDocument load(byte[] input, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias) throws java.io.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.java.io.IOException
- In case of a reading or parsing error.public static PDDocument load(byte[] input, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias, MemoryUsageSetting memUsageSetting) throws java.io.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.java.io.IOException
- In case of a reading or parsing error.public void save(java.lang.String fileName) throws java.io.IOException
fileName
- The file to save as.java.io.IOException
- if the output could not be writtenpublic void save(java.io.File file) throws java.io.IOException
file
- The file to save as.java.io.IOException
- if the output could not be writtenpublic void save(java.io.OutputStream output) throws java.io.IOException
output
- The stream to write to. It will be closed when done. It is recommended to wrap
it in a BufferedOutputStream
, unless it is already buffered.java.io.IOException
- if the output could not be writtenpublic void saveIncremental(java.io.OutputStream output) throws java.io.IOException
COSUpdateInfo.isNeedToBeUpdated()
set, starting from the document
catalog. For signatures this is taken care by PDFBox itself.
Other usages of this method are for experienced users only. You will usually never need it. It is useful only if you are required to keep the current revision and append the changes. A typical use case is changing a signed file without invalidating the signature.
output
- stream to write to. It will be closed when done. It
must never point to the source file or that one will be
harmed!java.io.IOException
- if the output could not be writtenjava.lang.IllegalStateException
- if the document was not loaded from a file or a stream.public ExternalSigningSupport saveIncrementalForExternalSigning(java.io.OutputStream output) throws java.io.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 the final PDF. It will be closed when the
document is closed. It must never point to the source file
or that one will be harmed!java.io.IOException
- if the output could not be writtenjava.lang.IllegalStateException
- if the document was not loaded from a file or a stream or
signature options were not set.public PDPage getPage(int pageIndex)
This method is too slow to get all the pages from a large PDF document
(1000 pages or more). For such documents, use the iterator of
getPages()
instead.
pageIndex
- the 0-based page indexpublic PDPageTree getPages()
public int getNumberOfPages()
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
- If there is an error releasing resources.public void protect(ProtectionPolicy policy) throws java.io.IOException
setAllSecurityToBeRemoved(boolean)
with a false argument if it was set to true
previously and logs a warning.policy
- The protection policy.java.io.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 java.lang.Long getDocumentId()
public void setDocumentId(java.lang.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.