Class PDDocument
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.PDDocument
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
PreflightDocument
public class PDDocument extends java.lang.Object implements java.io.Closeable
This is the in-memory representation of the PDF document. The #close() method must be called once the document is no longer needed.
-
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor 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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method 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.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 listprivate 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, 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.File file, MemoryUsageSetting memUsageSetting)
Parses a PDF.static PDDocument
load(java.io.InputStream input)
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.static PDDocument
load(java.io.InputStream input, 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.void
saveIncremental(java.io.OutputStream output, java.util.Set<COSDictionary> objectsToWrite)
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.
-
-
-
Field Detail
-
RESERVE_BYTE_RANGE
private static final 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. You'll need to fetch (withPDSignature.getByteRange()
) and reassign this yourself (withPDSignature.setByteRange(int[])
) only if you callsaveIncrementalForExternalSigning()
twice.
-
LOG
private static final org.apache.commons.logging.Log LOG
-
document
private final COSDocument document
-
documentInformation
private PDDocumentInformation documentInformation
-
documentCatalog
private PDDocumentCatalog documentCatalog
-
encryption
private PDEncryption encryption
-
allSecurityToBeRemoved
private boolean allSecurityToBeRemoved
-
documentId
private java.lang.Long documentId
-
pdfSource
private final RandomAccessRead pdfSource
-
accessPermission
private AccessPermission accessPermission
-
fontsToSubset
private final java.util.Set<PDFont> fontsToSubset
-
fontsToClose
private final java.util.Set<TrueTypeFont> fontsToClose
-
signInterface
private SignatureInterface signInterface
-
signingSupport
private SigningSupport signingSupport
-
resourceCache
private ResourceCache resourceCache
-
signatureAdded
private boolean signatureAdded
-
-
Constructor Detail
-
PDDocument
public PDDocument()
Creates an empty PDF document. You need to add at least one page for the document to be valid.
-
PDDocument
public PDDocument(MemoryUsageSetting memUsageSetting)
Creates an empty PDF document. You need to add at least one page for the document to be valid.- Parameters:
memUsageSetting
- defines how memory is used for buffering PDF streams
-
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.
-
PDDocument
public PDDocument(COSDocument doc, RandomAccessRead source)
Constructor that uses an existing document. The COSDocument that is passed in must be valid.- Parameters:
doc
- The COSDocument that this document wraps.source
- the parser which is used to read the pdf
-
PDDocument
public PDDocument(COSDocument doc, RandomAccessRead source, AccessPermission permission)
Constructor that uses an existing document. The COSDocument that is passed in must be valid.- Parameters:
doc
- The COSDocument that this document wraps.source
- the parser which is used to read the pdfpermission
- he access permissions of the pdf
-
-
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.
-
addSignature
public void addSignature(PDSignature sigObject) throws java.io.IOException
Add parameters of signature to be created externally using default signature options. SeesaveIncrementalForExternalSigning(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.
- Parameters:
sigObject
- is the PDSignatureField model- Throws:
java.io.IOException
- if there is an error creating required fieldsjava.lang.IllegalStateException
- if one attempts to add several signature fields.
-
addSignature
public void addSignature(PDSignature sigObject, SignatureOptions options) throws java.io.IOException
Add parameters of signature to be created externally. SeesaveIncrementalForExternalSigning(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.
- Parameters:
sigObject
- is the PDSignatureField modeloptions
- signature options- Throws:
java.io.IOException
- if there is an error creating required fieldsjava.lang.IllegalStateException
- if one attempts to add several signature fields.
-
addSignature
public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface) throws java.io.IOException
Add a signature to be created using the instance of given interface.Only one signature may be added in a document. To sign several times, load document, add signature, save incremental and close again.
- Parameters:
sigObject
- is the PDSignatureField modelsignatureInterface
- is an interface whose implementation provides signing capabilities. Can be null if external signing if used.- Throws:
java.io.IOException
- if there is an error creating required fieldsjava.lang.IllegalStateException
- if one attempts to add several signature fields.
-
addSignature
public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface, SignatureOptions options) throws java.io.IOException
This will add a signature to the document. If the 0-based page number in the options parameter is smaller than 0 or larger than max, the nearest valid page number will be used (i.e. 0 or max) and no exception will be thrown.Only one signature may be added in a document. To sign several times, load document, add signature, save incremental and close again.
- Parameters:
sigObject
- is the PDSignatureField modelsignatureInterface
- is an interface whose implementation provides signing capabilities. Can be null if external signing if used.options
- signature options- Throws:
java.io.IOException
- if there is an error creating required fieldsjava.lang.IllegalStateException
- if one attempts to add several signature fields.
-
findSignatureField
private PDSignatureField findSignatureField(java.util.Iterator<PDField> fieldIterator, PDSignature sigObject)
Search acroform fields for signature field with specific signature dictionary.- Parameters:
fieldIterator
- iterator on all fields.sigObject
- signature object (the /V part).- Returns:
- a signature field if found, or null if none was found.
-
checkSignatureField
private boolean checkSignatureField(java.util.Iterator<PDField> fieldIterator, PDSignatureField signatureField)
Check if the field already exists in the field list.- Parameters:
fieldIterator
- iterator on all fields.signatureField
- the signature field.- Returns:
- true if the field already existed in the field list, false if not.
-
checkSignatureAnnotation
private boolean checkSignatureAnnotation(java.util.List<PDAnnotation> annotations, PDAnnotationWidget widget)
Check if the widget already exists in the annotation list- Parameters:
annotations
- the list of PDAnnotation fields.widget
- the annotation widget.- Returns:
- true if the widget already existed in the annotation list, false if not.
-
prepareVisibleSignature
private void prepareVisibleSignature(PDSignatureField signatureField, PDAcroForm acroForm, COSDocument visualSignature)
-
assignSignatureRectangle
private void assignSignatureRectangle(PDSignatureField signatureField, COSDictionary annotDict)
-
assignAppearanceDictionary
private void assignAppearanceDictionary(PDSignatureField signatureField, COSDictionary apDict)
-
assignAcroFormDefaultResource
private void assignAcroFormDefaultResource(PDAcroForm acroForm, COSDictionary newDict)
-
prepareNonVisibleSignature
private void prepareNonVisibleSignature(PDSignatureField signatureField)
-
addSignatureField
@Deprecated public void addSignatureField(java.util.List<PDSignatureField> sigFields, SignatureInterface signatureInterface, SignatureOptions options) throws java.io.IOException
Deprecated.The method is misleading, because only one signature may be added in a document. The method will be removed in the future.This will add a list of signature fields to the document.- Parameters:
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 options- Throws:
java.io.IOException
- if there is an error creating required fields
-
removePage
public void removePage(PDPage page)
Remove the page from the document.- Parameters:
page
- The page to remove from the document.
-
removePage
public void removePage(int pageNumber)
Remove the page from the document.- Parameters:
pageNumber
- 0 based index to page number.
-
importPage
public PDPage importPage(PDPage page) throws java.io.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 theaddPage()
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.
- Parameters:
page
- The page to import.- Returns:
- The page that was imported.
- Throws:
java.io.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. If it doesn't exist, an empty document info dictionary is created in the document trailer.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()
.- Returns:
- The documents /Info dictionary, never null.
-
setDocumentInformation
public void setDocumentInformation(PDDocumentInformation info)
This will set the document information for this document.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)
.- 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.
-
getEncryption
public PDEncryption getEncryption()
This will get the encryption dictionary for this document. This will still return the parameters if the document was decrypted. As the encryption architecture in PDF documents is pluggable 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)
-
setEncryptionDictionary
public void setEncryptionDictionary(PDEncryption encryption) throws java.io.IOException
This will set the encryption dictionary for this document.- Parameters:
encryption
- The encryption dictionary(most likely a PDStandardEncryption object)- Throws:
java.io.IOException
- If there is an error determining which security handler to use.
-
getLastSignatureDictionary
public PDSignature getLastSignatureDictionary() throws java.io.IOException
This will return the last signature from the field tree. Note that this may not be the last in time when empty signature fields are created first but signed after other fields.- Returns:
- the last signature as
PDSignatureField
. - Throws:
java.io.IOException
- if no document catalog can be found.
-
getSignatureFields
public java.util.List<PDSignatureField> getSignatureFields() throws java.io.IOException
Retrieve all signature fields from the document.- Returns:
- a
List
ofPDSignatureField
s - Throws:
java.io.IOException
- if no document catalog can be found.
-
getSignatureDictionaries
public java.util.List<PDSignature> getSignatureDictionaries() throws java.io.IOException
Retrieve all signature dictionaries from the document.- Returns:
- a
List
ofPDSignatureField
s - Throws:
java.io.IOException
- if no document catalog can be found.
-
registerTrueTypeFontForClosing
public 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. Users don't have to call this method, it is done by the appropriate PDFont classes.- Parameters:
ttf
-
-
getFontsToSubset
java.util.Set<PDFont> getFontsToSubset()
Returns the list of fonts which will be subset before the document is saved.
-
load
public static PDDocument load(java.io.File file) throws java.io.IOException
Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.- Parameters:
file
- file to be loaded- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the file required a non-empty password.java.io.IOException
- in case of a file reading or parsing error
-
load
public static PDDocument load(java.io.File file, MemoryUsageSetting memUsageSetting) throws java.io.IOException
Parses a PDF.- Parameters:
file
- file to be loadedmemUsageSetting
- defines how memory is used for buffering PDF streams- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the file required a non-empty password.java.io.IOException
- in case of a file reading or parsing error
-
load
public static PDDocument load(java.io.File file, java.lang.String password) throws java.io.IOException
Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.- Parameters:
file
- file to be loadedpassword
- password to be used for decryption- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the password is incorrect.java.io.IOException
- in case of a file reading or parsing error
-
load
public static PDDocument load(java.io.File file, java.lang.String password, MemoryUsageSetting memUsageSetting) throws java.io.IOException
Parses a PDF.- Parameters:
file
- file to be loadedpassword
- password to be used for decryptionmemUsageSetting
- defines how memory is used for buffering PDF streams- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the password is incorrect.java.io.IOException
- in case of a file reading or parsing error
-
load
public static PDDocument load(java.io.File file, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias) throws java.io.IOException
Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.- Parameters:
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 security- Returns:
- loaded document
- Throws:
java.io.IOException
- in case of a file reading or parsing error
-
load
public 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
Parses a PDF.- Parameters:
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 streams- Returns:
- loaded document
- Throws:
java.io.IOException
- in case of a file reading or parsing error
-
load
private static PDDocument load(RandomAccessBufferedFileInputStream raFile, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias, MemoryUsageSetting memUsageSetting) throws java.io.IOException
- Throws:
java.io.IOException
-
load
public static PDDocument load(java.io.InputStream input) throws java.io.IOException
Parses a PDF. The given input stream is copied to the memory to enable random access to the pdf. Unrestricted main memory will be used for buffering PDF streams.- Parameters:
input
- stream that contains the document. Don't forget to close it after loading.- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the PDF required a non-empty password.java.io.IOException
- In case of a reading or parsing error.
-
load
public static PDDocument load(java.io.InputStream input, MemoryUsageSetting memUsageSetting) throws java.io.IOException
Parses a PDF. Depending on the memory settings parameter the given input stream is either copied to main memory or to a temporary file to enable random access to the pdf.- Parameters:
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 streams- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the PDF required a non-empty password.java.io.IOException
- In case of a reading or parsing error.
-
load
public static PDDocument load(java.io.InputStream input, java.lang.String password) throws java.io.IOException
Parses a PDF. The given input stream is copied to the memory to enable random access to the pdf. Unrestricted main memory will be used for buffering PDF streams.- Parameters:
input
- stream that contains the document. Don't forget to close it after loading.password
- password to be used for decryption- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the password is incorrect.java.io.IOException
- In case of a reading or parsing error.
-
load
public static PDDocument load(java.io.InputStream input, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias) throws java.io.IOException
Parses a PDF. The given input stream is copied to the memory to enable random access to the pdf. Unrestricted main memory will be used for buffering PDF streams.- Parameters:
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 security- Returns:
- loaded document
- Throws:
java.io.IOException
- In case of a reading or parsing error.
-
load
public static PDDocument load(java.io.InputStream input, java.lang.String password, MemoryUsageSetting memUsageSetting) throws java.io.IOException
Parses a PDF. Depending on the memory settings parameter the given input stream is either copied to main memory or to a temporary file to enable random access to the pdf.- Parameters:
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 streams- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the password is incorrect.java.io.IOException
- In case of a reading or parsing error.
-
load
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
Parses a PDF. Depending on the memory settings parameter the given input stream is either copied to memory or to a temporary file to enable random access to the pdf.- Parameters:
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 streams- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the password is incorrect.java.io.IOException
- In case of a reading or parsing error.
-
load
public static PDDocument load(byte[] input) throws java.io.IOException
Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.- Parameters:
input
- byte array that contains the document.- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the PDF required a non-empty password.java.io.IOException
- In case of a reading or parsing error.
-
load
public static PDDocument load(byte[] input, java.lang.String password) throws java.io.IOException
Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.- Parameters:
input
- byte array that contains the document.password
- password to be used for decryption- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the password is incorrect.java.io.IOException
- In case of a reading or parsing error.
-
load
public static PDDocument load(byte[] input, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias) throws java.io.IOException
Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.- Parameters:
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 security- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the password is incorrect.java.io.IOException
- In case of a reading or parsing error.
-
load
public static PDDocument load(byte[] input, java.lang.String password, java.io.InputStream keyStore, java.lang.String alias, MemoryUsageSetting memUsageSetting) throws java.io.IOException
Parses a PDF.- Parameters:
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 streams- Returns:
- loaded document
- Throws:
InvalidPasswordException
- If the password is incorrect.java.io.IOException
- In case of a reading or parsing error.
-
save
public void save(java.lang.String fileName) throws java.io.IOException
Save the document to a file.If encryption has been activated (with
protect(ProtectionPolicy)
), do not use the document after saving because the contents are now encrypted.- Parameters:
fileName
- The file to save as.- Throws:
java.io.IOException
- if the output could not be written
-
save
public void save(java.io.File file) throws java.io.IOException
Save the document to a file.If encryption has been activated (with
protect(ProtectionPolicy)
), do not use the document after saving because the contents are now encrypted.- Parameters:
file
- The file to save as.- Throws:
java.io.IOException
- if the output could not be written
-
save
public void save(java.io.OutputStream output) throws java.io.IOException
This will save the document to an output stream.If encryption has been activated (with
protect(ProtectionPolicy)
), do not use the document after saving because the contents are now encrypted.- Parameters:
output
- The stream to write to. It will be closed when done. It is recommended to wrap it in aBufferedOutputStream
, unless it is already buffered.- Throws:
java.io.IOException
- if the output could not be written
-
saveIncremental
public void saveIncremental(java.io.OutputStream output) throws java.io.IOException
Save the PDF as an incremental update. This is only possible if the PDF was loaded from a file or a stream, not if the document was created in PDFBox itself. There must be a path of objects that haveCOSUpdateInfo.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.
- Parameters:
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!- Throws:
java.io.IOException
- if the output could not be writtenjava.lang.IllegalStateException
- if the document was not loaded from a file or a stream.
-
saveIncremental
public void saveIncremental(java.io.OutputStream output, java.util.Set<COSDictionary> objectsToWrite) throws java.io.IOException
Save the PDF as an incremental update. This is only possible if the PDF was loaded from a file or a stream, not if the document was created in PDFBox itself. This allows to include objects even if there is no path of objects that haveCOSUpdateInfo.isNeedToBeUpdated()
set so the incremental update gets smaller. Only dictionaries are supported; if you need to update other objects classes, then add their parent dictionary.This method is 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. To know which objects are getting changed, you need to have some understanding of the PDF specification, and look at the saved file with an editor to verify that you are updating the correct objects. You should also inspect the page and document structures of the file with PDFDebugger.
- Parameters:
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!objectsToWrite
- objects that must be part of the incremental saving.- Throws:
java.io.IOException
- if the output could not be writtenjava.lang.IllegalStateException
- if the document was not loaded from a file or a stream.
-
saveIncrementalForExternalSigning
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 forPDDocument
instance and only AFTERExternalSigningSupport
instance is used.- Parameters:
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!- Returns:
- instance to be used for external signing and setting CMS signature
- Throws:
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.
-
getPage
public PDPage getPage(int pageIndex)
Returns the page at the given 0-based index.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.- Parameters:
pageIndex
- the 0-based page index- Returns:
- the page at the given index.
-
getPages
public PDPageTree getPages()
Returns the page tree.- Returns:
- the page tree
-
getNumberOfPages
public int getNumberOfPages()
This will return the total page count of the PDF document.- Returns:
- The total number of pages in the PDF document.
-
close
public void close() throws java.io.IOException
This will close the underlying COSDocument object.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
- If there is an error releasing resources.
-
protect
public void protect(ProtectionPolicy policy) throws java.io.IOException
Protects the document with a protection policy. The document content will be really encrypted when it will be saved. This method only marks the document for encryption. It also callssetAllSecurityToBeRemoved(boolean)
with a false argument if it was set to true previously and logs a warning.Do not use the document after saving, because the structures are encrypted.
- Parameters:
policy
- The protection policy.- Throws:
java.io.IOException
- if there isn't any suitable security handler.- See Also:
StandardProtectionPolicy
,PublicKeyProtectionPolicy
-
getCurrentAccessPermission
public AccessPermission getCurrentAccessPermission()
Returns the access permissions granted when the document was decrypted. If the document was not decrypted this method returns the access permission for a document owner (ie can do everything). The returned object is in read only mode so that permissions cannot be changed. Methods providing access to content should rely on this object to verify if the current user is allowed to proceed.- Returns:
- the access permissions for the current user on the document.
-
isAllSecurityToBeRemoved
public boolean isAllSecurityToBeRemoved()
Indicates if all security is removed or not when writing the pdf.- Returns:
- returns true if all security shall be removed otherwise false
-
setAllSecurityToBeRemoved
public void setAllSecurityToBeRemoved(boolean removeAllSecurity)
Activates/Deactivates the removal of all security when writing the pdf.- Parameters:
removeAllSecurity
- remove all security if set to true
-
getDocumentId
public java.lang.Long getDocumentId()
Provides the document ID.- Returns:
- the dcoument ID
-
setDocumentId
public void setDocumentId(java.lang.Long docId)
Sets the document ID to the given value.- Parameters:
docId
- the new document ID
-
getVersion
public float getVersion()
Returns the PDF specification version this document conforms to.- Returns:
- the PDF version (e.g. 1.4f)
-
setVersion
public void setVersion(float newVersion)
Sets the PDF specification version for this document.- Parameters:
newVersion
- the new PDF version (e.g. 1.4f)
-
getResourceCache
public ResourceCache getResourceCache()
Returns the resource cache associated with this document, or null if there is none.- Returns:
- the resource cache or null.
-
setResourceCache
public void setResourceCache(ResourceCache resourceCache)
Sets the resource cache associated with this document.- Parameters:
resourceCache
- A resource cache, or null.
-
-