Top Description Fields Constructors Methods
org.apache.pdfbox.pdmodel

public Class PDDocument

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Imports
java.awt.Point, java.awt.image.DataBuffer, .Raster, .WritableRaster, java.io.BufferedOutputStream, .Closeable, .File, .FileOutputStream, .IOException, .OutputStream, java.util.ArrayList, .Arrays, .Collection, .HashSet, .Iterator, .List, .Set, java.util.stream.Collectors, org.apache.commons.logging.Log, .LogFactory, org.apache.fontbox.ttf.TrueTypeFont, org.apache.pdfbox.cos.COSArray, .COSBase, .COSDictionary, .COSDocument, .COSInteger, .COSName, .COSObject, .COSObjectKey, .COSUpdateInfo, org.apache.pdfbox.io.IOUtils, .RandomAccessRead, .RandomAccessStreamCache.StreamCacheCreateFunction, org.apache.pdfbox.pdfwriter.COSWriter, org.apache.pdfbox.pdfwriter.compress.CompressParameters, org.apache.pdfbox.pdmodel.common.COSArrayList, .PDRectangle, .PDStream, org.apache.pdfbox.pdmodel.encryption.AccessPermission, .PDEncryption, .ProtectionPolicy, .SecurityHandler, .SecurityHandlerFactory, org.apache.pdfbox.pdmodel.font.PDFont, org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB, org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation, .PDAnnotationWidget, .PDAppearanceDictionary, .PDAppearanceStream, org.apache.pdfbox.pdmodel.interactive.digitalsignature.ExternalSigningSupport, .PDSignature, .SignatureInterface, .SignatureOptions, .SigningSupport, org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm, .PDField, .PDSignatureField

This is the in-memory representation of the PDF document. The #close() method must be called once the document is no longer needed.
Author
Ben Litchfield

Field Summary

Modifier and TypeField and Description
private AccessPermission
private boolean
private final COSDocument
private PDDocumentCatalog
private Long
private PDDocumentInformation
private PDEncryption
private final Set<TrueTypeFont>
private final Set<PDFont>
private final Collection<COSObjectKey>
private static final Log
private final RandomAccessRead
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.

private ResourceCache
private boolean
private SigningSupport
private SignatureInterface

Constructor Summary

AccessConstructor and Description
public
PDDocument()

Creates an empty PDF document.

public
PDDocument(RandomAccessStreamCache.StreamCacheCreateFunction
a function to create an instance of a stream cache for buffering PDF streams
streamCacheCreateFunction
)

Creates an empty PDF document.

public
PDDocument(COSDocument
The COSDocument that this document wraps.
doc
)

Constructor that uses an existing document.

public
PDDocument(COSDocument
The COSDocument that this document wraps.
doc
,
RandomAccessRead
input representing the pdf
source
)

Constructor that uses an existing document.

public
PDDocument(COSDocument
The COSDocument that this document wraps.
doc
,
RandomAccessRead
input representing the pdf
source
,
AccessPermission
he access permissions of the pdf
permission
)

Constructor that uses an existing document.

Method Summary

Modifier and TypeMethod and Description
public void
addPage(PDPage
The page to add to the document.
page
)

This will add a page to the document.

public void
addSignature(PDSignature
is the PDSignatureField model
sigObject
)

Add parameters of signature to be created externally using default signature options.

public void
addSignature(PDSignature
is the PDSignatureField model
sigObject
,
SignatureOptions
signature options
options
)

Add parameters of signature to be created externally.

public void
addSignature(PDSignature
is the PDSignatureField model
sigObject
,
SignatureInterface
is an interface whose implementation provides signing capabilities. Can be null if external signing if used.
signatureInterface
)

Add a signature to be created using the instance of given interface.

public void
addSignature(PDSignature
is the PDSignatureField model
sigObject
,
SignatureInterface
is an interface whose implementation provides signing capabilities. Can be null if external signing if used.
signatureInterface
,
SignatureOptions
signature options
options
)

This will add a signature to the document.

private void
private void
private void
private boolean

Returns:

true if the widget already existed in the annotation list, false if not.
checkSignatureAnnotation
(List<PDAnnotation>
the list of PDAnnotation fields.
annotations
,
PDAnnotationWidget
the annotation widget.
widget
)

Check if the widget already exists in the annotation list.

private boolean

Returns:

true if the field already existed in the field list, false if not.
checkSignatureField
(Iterator<PDField>
iterator on all fields.
fieldIterator
,
PDSignatureField
the signature field.
signatureField
)

Check if the field already exists in the field list.

public void
close()

Implements java.io.Closeable.close.

This will close the underlying COSDocument object.

private PDSignatureField

Returns:

a signature field if found, or null if none was found.
findSignatureField
(Iterator<PDField>
iterator on all fields.
fieldIterator
,
PDSignature
signature object (the /V part).
sigObject
)

Search acroform fields for signature field with specific signature dictionary.

public AccessPermission

Returns:

the access permissions for the current user on the document.
getCurrentAccessPermission
()

Returns the access permissions granted when the document was decrypted.

public COSDocument

Returns:

The document that this layer sits on top of.
getDocument
()

This will get the low level document.

public PDDocumentCatalog

Returns:

The documents /Root dictionary
getDocumentCatalog
()

This will get the document CATALOG.

public Long

Returns:

the document ID
getDocumentId
()

Provides the document ID. This is not the trailer document ID but the time used to create it.

public PDDocumentInformation

Returns:

The documents /Info dictionary, never null.
getDocumentInformation
()

This will get the document info dictionary.

public PDEncryption

Returns:

The encryption dictionary(most likely a PDStandardEncryption object)
getEncryption
()

This will get the encryption dictionary for this document.

pack-priv Set<PDFont>
getFontsToSubset()

Returns the list of fonts which will be subset before the document is saved.

public PDSignature

Returns:

the last signature as PDSignatureField.
getLastSignatureDictionary
()

This will return the last signature from the field tree.

public int

Returns:

The total number of pages in the PDF document.
getNumberOfPages
()

This will return the total page count of the PDF document.

public PDPage

Returns:

the page at the given index.
getPage
(int
the 0-based page index
pageIndex
)

Returns the page at the given 0-based index.

public PDPageTree

Returns:

the page tree
getPages
()

Returns the page tree.

public ResourceCache

Returns:

the resource cache of the document
getResourceCache
()

Returns the resource cache associated with this document, or null if there is none.

public List<PDSignature>

Returns:

a List of PDSignatureFields
getSignatureDictionaries
()

Retrieve all signature dictionaries from the document.

public List<PDSignatureField>

Returns:

a List of PDSignatureFields
getSignatureFields
()

Retrieve all signature fields from the document.

public float

Returns:

the PDF version (e.g. 1.4f)
getVersion
()

Returns the PDF specification version this document conforms to.

public PDPage

Returns:

The page that was imported.
importPage
(PDPage
The page to import.
page
)

This will import and copy the contents from another location.

public boolean

Returns:

returns true if all security shall be removed otherwise false
isAllSecurityToBeRemoved
()

Indicates if all security is removed or not when writing the pdf.

public boolean

Returns:

true If this document is encrypted.
isEncrypted
()

This will tell if this document is encrypted or not.

private void
private void
prepareVisibleSignature(PDAnnotationWidget firstWidget, PDAcroForm acroForm, COSDocument visualSignature)

public void
protect(ProtectionPolicy
The protection policy.
policy
)

Protects the document with a protection policy.

public void
registerTrueTypeFontForClosing(TrueTypeFont
the TrueTypeFont to be registered
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.

public void
removePage(PDPage
The page to remove from the document.
page
)

Remove the page from the document.

public void
removePage(int
0 based index to page number.
pageNumber
)

Remove the page from the document.

public void
save(String
The file to save as.
fileName
)

Save the document to a file using default compression.

public void
save(File
The file to save as.
file
)

Save the document to a file using default compression.

public void
save(OutputStream
The stream to write to. It is recommended to wrap it in a java.io.BufferedOutputStream, unless it is already buffered.
output
)

This will save the document to an output stream.

public void
save(File
The file to save as.
file
,
CompressParameters
The parameters for the document's compression.
compressParameters
)

Save the document using the given compression.

public void
save(String
The file to save as.
fileName
,
CompressParameters
The parameters for the document's compression.
compressParameters
)

Save the document to a file using the given compression.

public void
save(OutputStream
The stream to write to. It is recommended to wrap it in a java.io.BufferedOutputStream, unless it is already buffered.
output
,
CompressParameters
The parameters for the document's compression.
compressParameters
)

Save the document using the given compression.

public void
saveIncremental(OutputStream
stream to write to. It will be closed when done. It must never point to the source file or that one will be harmed!
output
)

Save the PDF as an incremental update.

public void
saveIncremental(OutputStream
stream to write to. It will be closed when done. It must never point to the source file or that one will be harmed!
output
,
Set<COSDictionary>
objects that must be part of the incremental saving.
objectsToWrite
)

Save the PDF as an incremental update.

public ExternalSigningSupport

Returns:

instance to be used for external signing and setting CMS signature
saveIncrementalForExternalSigning
(OutputStream
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!
output
)

Save PDF incrementally without closing for external signature creation scenario.

public void
setAllSecurityToBeRemoved(boolean
remove all security if set to true
removeAllSecurity
)

Activates/Deactivates the removal of all security when writing the pdf.

public void
setDocumentId(Long
the new document ID
docId
)

Sets the document ID to the given value.

public void
setDocumentInformation(PDDocumentInformation
The updated document information.
info
)

This will set the document information for this document.

public void
setEncryptionDictionary(PDEncryption
The encryption dictionary(most likely a PDStandardEncryption object)
encryption
)

This will set the encryption dictionary for this document.

private void
setHighestImportedObjectNumber(PDPage
the imported page.
importedPage
)

Determine the highest object number from the imported page to avoid mixed up numbers when saving the new pdf.

public void
setResourceCache(ResourceCache
A resource cache, or null.
resourceCache
)

Sets the resource cache associated with this document.

public void
setVersion(float
the new PDF version (e.g. 1.4f)
newVersion
)

Sets the PDF specification version for this document.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait