Class PDDocument

java.lang.Object
org.sejda.sambox.pdmodel.PDDocument
All Implemented Interfaces:
Closeable, AutoCloseable

public class PDDocument extends Object implements Closeable
This is the in-memory representation of the PDF document.
Author:
Ben Litchfield
  • Constructor Details

    • PDDocument

      public PDDocument()
    • PDDocument

      public PDDocument(COSDocument document)
      Constructor that uses an existing document. The COSDocument that is passed in must be valid.
      Parameters:
      document - The COSDocument that this document wraps.
    • PDDocument

      public PDDocument(COSDocument document, SecurityHandler securityHandler)
      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

      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.
    • 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.
    • getDocument

      public COSDocument getDocument()
      Returns:
      The document that this layer sits on top of.
    • getDocumentInformation

      public PDDocumentInformation getDocumentInformation()
      This will get the document info dictionary. This is guaranteed to not return null.
      Returns:
      The documents /Info dictionary
    • setDocumentInformation

      public void setDocumentInformation(PDDocumentInformation documentInformation)
      This will set the document information for this document.
      Parameters:
      documentInformation - 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()
      Returns:
      true If this document is encrypted.
    • getEncryption

      public PDEncryption 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

      public Set<Subsettable> getFontsToSubset()
      Returns:
      the list of fonts which will be subset before the document is saved.
    • getPage

      public PDPage getPage(int pageIndex)
      Parameters:
      pageIndex - the page index
      Returns:
      the page at the given zero based index.
    • getPages

      public PDPageTree getPages()
    • getNumberOfPages

      public int getNumberOfPages()
      Returns:
      The total number of pages in the PDF document.
    • 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.
    • getSecurityHandler

      public SecurityHandler getSecurityHandler()
    • getVersion

      public String getVersion()
      Returns:
      The version of the PDF specification to which the document conforms.
    • setVersion

      public void setVersion(String newVersion)
      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

      public void requireMinVersion(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.
      Parameters:
      version -
    • setOnCloseAction

      @Deprecated public void setOnCloseAction(PDDocument.OnClose onClose)
      Deprecated.
      Deprecated since the name suggests a setter while we actually have PDDocument.OnClose actions composition (we don't set the action, we add it to existing actions).
      Parameters:
      onClose -
    • addOnCloseAction

      public void addOnCloseAction(PDDocument.OnClose onClose)
      Adds the given PDDocument.OnClose to the set of actions to be executed right before this PDDocument is closed.
      Parameters:
      onClose -
    • setOnBeforeWriteAction

      public void setOnBeforeWriteAction(PDDocument.OnBeforeWrite onBeforeWrite)
    • generateFileIdentifier

      public COSString generateFileIdentifier(byte[] md5Update)
      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

      public void writeTo(File file, WriteOption... options) throws IOException
      Writes the document to the given File. The document is closed once written.
      Parameters:
      file -
      options -
      Throws:
      IOException
      See Also:
    • writeTo

      public void writeTo(String filename, WriteOption... options) throws IOException
      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

      public void writeTo(WritableByteChannel channel, WriteOption... options) throws IOException
      Writes the document to the given WritableByteChannel. The document is closed once written.
      Parameters:
      channel -
      options -
      Throws:
      IOException
      See Also:
    • writeTo

      public void writeTo(OutputStream out, WriteOption... options) throws IOException
      Writes the document to the given OutputStream. 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 given File encrypting 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 given WritableByteChannel encrypting 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 given OutputStream encrypting 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 PDDocument is open
    • close

      public void close() throws IOException
      Closes the PDDocument executing the set onClose action. Once closed the document is pretty much unusable since most of the methods requires an open document.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
      See Also:
    • getResourceCache

      public ResourceCache getResourceCache()
      Returns the resource cache associated with this document, or null if there is none.
    • load

      public static PDDocument load(File file) throws IOException
      Throws:
      IOException
    • load

      public static PDDocument load(String path) throws IOException
      Throws:
      IOException
    • save

      public void save(String path) throws IOException
      Throws:
      IOException
    • hasParseErrors

      public boolean hasParseErrors()
    • assertNumberOfPagesIsAccurate

      public void assertNumberOfPagesIsAccurate()