java.lang.Object
org.sejda.sambox.pdmodel.PDDocument
- All Implemented Interfaces:
Closeable,AutoCloseable
This is the in-memory representation of the PDF document.
- Author:
- Ben Litchfield
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAction to be performed right before thisPDDocumentis written to a file.static interfaceAction to be performed before thePDDocumentis close -
Constructor Summary
ConstructorsConstructorDescriptionPDDocument(COSDocument document) Constructor that uses an existing document.PDDocument(COSDocument document, SecurityHandler securityHandler) Constructor that uses an existing document. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddOnCloseAction(PDDocument.OnClose onClose) Adds the givenPDDocument.OnCloseto the set of actions to be executed right before thisPDDocumentis closed.voidThis will add a page to the document.voidvoidclose()Closes thePDDocumentexecuting the set onClose action.generateFileIdentifier(byte[] md5Update) Returns the access permissions granted when the document was decrypted.This will get the document CATALOG.This will get the document info dictionary.This will get the encryption dictionary for this document.intgetPage(int pageIndex) getPages()Returns the resource cache associated with this document, or null if there is none.booleanbooleanbooleanisOpen()static PDDocumentstatic PDDocumentvoidregisterTrueTypeFontForClosing(org.apache.fontbox.ttf.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.voidremovePage(int pageNumber) Remove the page from the document.voidremovePage(PDPage page) Remove the page from the document.voidrequireMinVersion(String version) If the document is not at the given version or above, it sets the version of the PDF specification to which the document conforms.voidvoidsetDocumentInformation(PDDocumentInformation documentInformation) This will set the document information for this document.voidsetOnBeforeWriteAction(PDDocument.OnBeforeWrite onBeforeWrite) voidsetOnCloseAction(PDDocument.OnClose onClose) Deprecated.voidsetVersion(String newVersion) Sets the version of the PDF specification to which the document conforms.voidwriteTo(File file, StandardSecurity security, WriteOption... options) Writes the document to the givenFileencrypting it using the given security.voidwriteTo(File file, WriteOption... options) Writes the document to the givenFile.voidwriteTo(OutputStream out, StandardSecurity security, WriteOption... options) Writes the document to the givenOutputStreamencrypting it using the given security.voidwriteTo(OutputStream out, WriteOption... options) Writes the document to the givenOutputStream.voidwriteTo(String filename, StandardSecurity security, WriteOption... options) Writes the document to the file corresponding the given file name encrypting it using the given security.voidwriteTo(String filename, WriteOption... options) Writes the document to the file corresponding the given file name.voidwriteTo(WritableByteChannel channel, StandardSecurity security, WriteOption... options) Writes the document to the givenWritableByteChannelencrypting it using the given security.voidwriteTo(WritableByteChannel channel, WriteOption... options) Writes the document to the givenWritableByteChannel.
-
Constructor Details
-
PDDocument
public PDDocument() -
PDDocument
Constructor that uses an existing document. The COSDocument that is passed in must be valid.- Parameters:
document- The COSDocument that this document wraps.
-
PDDocument
Constructor that uses an existing document. The COSDocument that is passed in must be valid.- Parameters:
document- The COSDocument that this document wraps.securityHandler-
-
-
Method Details
-
addPage
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.
-
removePage
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.
-
getDocument
- Returns:
- The document that this layer sits on top of.
-
getDocumentInformation
This will get the document info dictionary. This is guaranteed to not return null.- Returns:
- The documents /Info dictionary
-
setDocumentInformation
This will set the document information for this document.- Parameters:
documentInformation- The updated document information.
-
getDocumentCatalog
This will get the document CATALOG. This is guaranteed to not return null.- Returns:
- The documents /Root dictionary
-
isEncrypted
public boolean isEncrypted()- Returns:
- true If this document is encrypted.
-
getEncryption
This will get the encryption dictionary for this document.- Returns:
- The encryption dictionary
-
registerTrueTypeFontForClosing
public void registerTrueTypeFontForClosing(org.apache.fontbox.ttf.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
- Returns:
- the list of fonts which will be subset before the document is saved.
-
getPage
- Parameters:
pageIndex- the page index- Returns:
- the page at the given zero based index.
-
getPages
-
getNumberOfPages
public int getNumberOfPages()- Returns:
- The total number of pages in the PDF document.
-
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.
-
getSecurityHandler
-
getVersion
- Returns:
- The version of the PDF specification to which the document conforms.
-
setVersion
Sets the version of the PDF specification to which the document conforms. Downgrading of the document version is not allowed.- Parameters:
newVersion- the new PDF version
-
requireMinVersion
If the document is not at the given version or above, it sets the version of the PDF specification to which the document conforms.- Parameters:
version-
-
setOnCloseAction
Deprecated.Deprecated since the name suggests a setter while we actually havePDDocument.OnCloseactions composition (we don't set the action, we add it to existing actions).- Parameters:
onClose-
-
addOnCloseAction
Adds the givenPDDocument.OnCloseto the set of actions to be executed right before thisPDDocumentis closed.- Parameters:
onClose-
-
setOnBeforeWriteAction
-
generateFileIdentifier
- Parameters:
md5Update-- Returns:
- a newly generated ID based on the input bytes, current timestamp and some other information, to be used as value of the ID array in the document trailer.
-
writeTo
Writes the document to the givenFile. The document is closed once written.- Parameters:
file-options-- Throws:
IOException- See Also:
-
writeTo
Writes the document to the file corresponding the given file name. The document is closed once written.- Parameters:
filename-options-- Throws:
IOException- See Also:
-
writeTo
Writes the document to the givenWritableByteChannel. The document is closed once written.- Parameters:
channel-options-- Throws:
IOException- See Also:
-
writeTo
Writes the document to the givenOutputStream. The document is closed once written.- Parameters:
out-options-- Throws:
IOException- See Also:
-
writeTo
public void writeTo(File file, StandardSecurity security, WriteOption... options) throws IOException Writes the document to the givenFileencrypting it using the given security. The document is closed once written.- Parameters:
file-security-options-- Throws:
IOException- See Also:
-
writeTo
public void writeTo(String filename, StandardSecurity security, WriteOption... options) throws IOException Writes the document to the file corresponding the given file name encrypting it using the given security. The document is closed once written.- Parameters:
filename-security-options-- Throws:
IOException- See Also:
-
writeTo
public void writeTo(WritableByteChannel channel, StandardSecurity security, WriteOption... options) throws IOException Writes the document to the givenWritableByteChannelencrypting it using the given security. The document is closed once written.- Parameters:
channel-security-options-- Throws:
IOException- See Also:
-
writeTo
public void writeTo(OutputStream out, StandardSecurity security, WriteOption... options) throws IOException Writes the document to the givenOutputStreamencrypting it using the given security. The document is closed once written.- Parameters:
out-security-options-- Throws:
IOException- See Also:
-
isOpen
public boolean isOpen()- Returns:
- true if the
PDDocumentis open
-
close
Closes thePDDocumentexecuting the set onClose action. Once closed the document is pretty much unusable since most of the methods requires an open document.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- See Also:
-
getResourceCache
Returns the resource cache associated with this document, or null if there is none. -
load
- Throws:
IOException
-
load
- Throws:
IOException
-
save
- Throws:
IOException
-
hasParseErrors
public boolean hasParseErrors() -
assertNumberOfPagesIsAccurate
public void assertNumberOfPagesIsAccurate()
-