Class PdfDocument

java.lang.Object
com.gitlab.pdftk_java.com.lowagie.text.Document
com.gitlab.pdftk_java.com.lowagie.text.pdf.PdfDocument
All Implemented Interfaces:
DocListener, ElementListener, EventListener

class PdfDocument extends Document implements DocListener
PdfDocument is the class that is used by PdfWriter to translate a Document into a PDF with different pages.

A PdfDocument always listens to a Document and adds the Pdf representation of every Element that is added to the Document.

See Also:
  • Field Details

    • thumb

      private PdfIndirectReference thumb
    • hangingPunctuation

      static final String hangingPunctuation
      The characters to be applied the hanging ponctuation.
      See Also:
    • writer

      private PdfWriter writer
      The PdfWriter.
    • info

      private PdfDocument.PdfInfo info
      some meta information about the Document.
    • firstPageEvent

      private boolean firstPageEvent
      Signals that OnOpenDocument should be called.
    • isParagraph

      private boolean isParagraph
      Signals that onParagraph is valid.
    • line

      private PdfLine line
      The line that is currently being written.
    • indentLeft

      private float indentLeft
      This represents the current indentation of the PDF Elements on the left side.
    • indentRight

      private float indentRight
      This represents the current indentation of the PDF Elements on the right side.
    • listIndentLeft

      private float listIndentLeft
      This represents the current indentation of the PDF Elements on the left side.
    • alignment

      private int alignment
      This represents the current alignment of the PDF Elements.
    • text

      private PdfContentByte text
      This is the PdfContentByte object, containing the text.
    • graphics

      private PdfContentByte graphics
      This is the PdfContentByte object, containing the borders and other Graphics.
    • lines

      private ArrayList lines
      The lines that are written until now.
    • leading

      private float leading
      This represents the leading of the lines.
    • currentHeight

      private float currentHeight
      This is the current height of the document.
    • indentTop

      private float indentTop
      This represents the current indentation of the PDF Elements on the top side.
    • indentBottom

      private float indentBottom
      This represents the current indentation of the PDF Elements on the bottom side.
    • pageEmpty

      private boolean pageEmpty
      This checks if the page is empty.
    • textEmptySize

      private int textEmptySize
    • nextPageSize

      protected Rectangle nextPageSize
      This is the size of the next page.
    • thisBoxSize

      protected HashMap thisBoxSize
      This is the size of the several boxes of the current Page.
    • boxSize

      protected HashMap boxSize
      This is the size of the several boxes that will be used in the next page.
    • pageResources

      protected PageResources pageResources
      This are the page resources of the current Page.
    • imageEnd

      private float imageEnd
      This is the position where the image ends.
    • imageIndentLeft

      private float imageIndentLeft
      This is the indentation caused by an image on the left.
    • imageIndentRight

      private float imageIndentRight
      This is the indentation caused by an image on the right.
    • annotations

      private ArrayList annotations
      This is the array containing the references to the annotations.
    • delayedAnnotations

      private ArrayList delayedAnnotations
      This is an array containg references to some delayed annotations.
    • acroForm

      private PdfAcroForm acroForm
      This is the AcroForm object.
    • rootOutline

      private PdfOutline rootOutline
      This is the root outline of the document.
    • currentOutline

      private PdfOutline currentOutline
      This is the current PdfOutline in the hierarchy of outlines.
    • currentAction

      private PdfAction currentAction
      The current active PdfAction when processing an Anchor.
    • localDestinations

      private TreeMap localDestinations
      Stores the destinations keyed by name. Value is Object[]{PdfAction,PdfIndirectReference,PdfDestintion}.
    • documentJavaScript

      private ArrayList documentJavaScript
    • viewerPreferences

      private int viewerPreferences
      these are the viewerpreferences of the document
    • openActionName

      private String openActionName
    • openActionAction

      private PdfAction openActionAction
    • additionalActions

      private PdfDictionary additionalActions
    • pageLabels

      private PdfPageLabels pageLabels
    • isNewpage

      private boolean isNewpage
    • paraIndent

      private float paraIndent
    • nextMarginLeft

      protected float nextMarginLeft
      margin in x direction starting from the left. Will be valid in the next page
    • nextMarginRight

      protected float nextMarginRight
      margin in x direction starting from the right. Will be valid in the next page
    • nextMarginTop

      protected float nextMarginTop
      margin in y direction starting from the top. Will be valid in the next page
    • nextMarginBottom

      protected float nextMarginBottom
      margin in y direction starting from the bottom. Will be valid in the next page
    • duration

      protected int duration
      The duration of the page
    • transition

      protected PdfTransition transition
      The page transition
    • pageAA

      protected PdfDictionary pageAA
    • strictImageSequence

      private boolean strictImageSequence
      Holds value of property strictImageSequence.
    • lastElementType

      private int lastElementType
      Holds the type of the last element, that has been added to the document.
    • markPoint

      protected int markPoint
  • Constructor Details

    • PdfDocument

      public PdfDocument()
      Constructs a new PDF document.
      Throws:
      DocumentException - on error
  • Method Details

    • setWriter

      public void setWriter(PdfWriter writer) throws DocumentException
      Adds a PdfWriter to the PdfDocument.
      Parameters:
      writer - the PdfWriter that writes everything what is added to this document to an outputstream.
      Throws:
      DocumentException - on error
    • setPageSize

      public boolean setPageSize(Rectangle pageSize)
      Sets the pagesize.
      Specified by:
      setPageSize in interface DocListener
      Overrides:
      setPageSize in class Document
      Parameters:
      pageSize - the new pagesize
      Returns:
      true if the page size was set
    • resetPageCount

      public void resetPageCount()
      Sets the page number to 0.
      Specified by:
      resetPageCount in interface DocListener
      Overrides:
      resetPageCount in class Document
    • setPageCount

      public void setPageCount(int pageN)
      Sets the page number.
      Specified by:
      setPageCount in interface DocListener
      Overrides:
      setPageCount in class Document
      Parameters:
      pageN - the new page number
    • setMargins

      public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
      Sets the margins.
      Specified by:
      setMargins in interface DocListener
      Overrides:
      setMargins in class Document
      Parameters:
      marginLeft - the margin on the left
      marginRight - the margin on the right
      marginTop - the margin on the top
      marginBottom - the margin on the bottom
      Returns:
      a boolean
    • rotateAnnotations

      protected PdfArray rotateAnnotations() throws DocumentException
      Throws:
      DocumentException
    • newPage

      public boolean newPage() throws DocumentException
      Makes a new page and sends it to the PdfWriter.
      Specified by:
      newPage in interface DocListener
      Overrides:
      newPage in class Document
      Returns:
      a boolean
      Throws:
      DocumentException - on error
    • open

      public void open()
      Opens the document.

      You have to open the document before you can begin to add content to the body of the document.

      Specified by:
      open in interface DocListener
      Overrides:
      open in class Document
    • outlineTree

      void outlineTree(PdfOutline outline) throws IOException
      Throws:
      IOException
    • writeOutlines

      void writeOutlines() throws IOException
      Throws:
      IOException
    • traverseOutlineCount

      void traverseOutlineCount(PdfOutline outline)
    • calculateOutlineCount

      void calculateOutlineCount()
    • close

      public void close()
      Closes the document. Once all the content has been written in the body, you have to close the body. After that nothing can be written to the body anymore.
      Specified by:
      close in interface DocListener
      Overrides:
      close in class Document
    • getPageResources

      PageResources getPageResources()
    • add

      public boolean add(Element element) throws DocumentException
      Signals that an Element was added to the Document.
      Specified by:
      add in interface ElementListener
      Overrides:
      add in class Document
      Parameters:
      element - the element to add
      Returns:
      true if the element was added, false if not.
      Throws:
      DocumentException - when a document isn't open yet, or has been closed
    • initPage

      private void initPage() throws DocumentException
      Initializes a page.

      If the footer/header is set, it is printed.

      Throws:
      DocumentException - on error
    • carriageReturn

      private void carriageReturn() throws DocumentException
      If the current line is not empty or null, it is added to the arraylist of lines and a new empty line is added.
      Throws:
      DocumentException - on error
    • newLine

      private void newLine() throws DocumentException
      Adds the current line to the list of lines and also adds an empty line.
      Throws:
      DocumentException - on error
    • flushLines

      private float flushLines() throws DocumentException
      Writes all the lines to the text-object.
      Returns:
      the displacement that was caused
      Throws:
      DocumentException - on error
    • getInfo

      Gets the PdfInfo-object.
      Returns:
      PdfInfo
    • getCatalog

      Gets the PdfCatalog-object.
      Parameters:
      pages - an indirect reference to this document pages
      Returns:
      PdfCatalog
      Throws:
      DocumentException
    • getVerticalPosition

      public float getVerticalPosition(boolean ensureNewLine)
      Gets the current vertical page position.
      Parameters:
      ensureNewLine - Tells whether a new line shall be enforced. This may cause side effects for elements that do not terminate the lines they've started because those lines will get terminated.
      Returns:
      The current vertical page position.
    • ensureNewLine

      private void ensureNewLine()
      Ensures that a new line has been started.
    • indentLeft

      private float indentLeft()
      Gets the indentation on the left side.
      Returns:
      a margin
    • indentRight

      private float indentRight()
      Gets the indentation on the right side.
      Returns:
      a margin
    • indentTop

      private float indentTop()
      Gets the indentation on the top side.
      Returns:
      a margin
    • indentBottom

      float indentBottom()
      Gets the indentation on the bottom side.
      Returns:
      a margin
    • addOutline

      void addOutline(PdfOutline outline, String name)
      Adds a named outline to the document .
      Parameters:
      outline - the outline to be added
      name - the name of this local destination
    • getAcroForm

      public PdfAcroForm getAcroForm() throws DocumentException
      Gets the AcroForm object.
      Returns:
      the PdfAcroform object of the PdfDocument
      Throws:
      DocumentException
    • getRootOutline

      public PdfOutline getRootOutline()
      Gets the root outline. All the outlines must be created with a parent. The first level is created with this outline.
      Returns:
      the root outline
    • writeLineToContent

      void writeLineToContent(PdfLine line, PdfContentByte text, PdfContentByte graphics, Object[] currentValues, float ratio) throws DocumentException
      Writes a text line to the document. It takes care of all the attributes.

      Before entering the line position must have been established and the text argument must be in text object scope (beginText()).

      Parameters:
      line - the line to be written
      text - the PdfContentByte where the text will be written to
      graphics - the PdfContentByte where the graphics will be written to
      currentValues - the current font and extra spacing values
      ratio -
      Throws:
      DocumentException - on error
    • localGoto

      void localGoto(String name, float llx, float lly, float urx, float ury)
      Implements a link to other part of the document. The jump will be made to a local destination with the same name, that must exist.
      Parameters:
      name - the name for this link
      llx - the lower left x corner of the activation area
      lly - the lower left y corner of the activation area
      urx - the upper right x corner of the activation area
      ury - the upper right y corner of the activation area
    • getLocalGotoAction

      PdfAction getLocalGotoAction(String name)
    • localDestination

      boolean localDestination(String name, PdfDestination destination)
      The local destination to where a local goto with the same name will jump to.
      Parameters:
      name - the name of this local destination
      destination - the PdfDestination with the jump coordinates
      Returns:
      true if the local destination was added, false if a local destination with the same name already existed
    • remoteGoto

      void remoteGoto(String filename, String name, float llx, float lly, float urx, float ury)
      Implements a link to another document.
      Parameters:
      filename - the filename for the remote document
      name - the name to jump to
      llx - the lower left x corner of the activation area
      lly - the lower left y corner of the activation area
      urx - the upper right x corner of the activation area
      ury - the upper right y corner of the activation area
    • remoteGoto

      void remoteGoto(String filename, int page, float llx, float lly, float urx, float ury)
      Implements a link to another document.
      Parameters:
      filename - the filename for the remote document
      page - the page to jump to
      llx - the lower left x corner of the activation area
      lly - the lower left y corner of the activation area
      urx - the upper right x corner of the activation area
      ury - the upper right y corner of the activation area
    • setViewerPreferences

      public void setViewerPreferences(int preferences)
      Sets the viewer preferences as the sum of several constants.
      Parameters:
      preferences - the viewer preferences
      See Also:
    • setAction

      void setAction(PdfAction action, float llx, float lly, float urx, float ury)
      Implements an action in an area.
      Parameters:
      action - the PdfAction
      llx - the lower left x corner of the activation area
      lly - the lower left y corner of the activation area
      urx - the upper right x corner of the activation area
      ury - the upper right y corner of the activation area
    • setOpenAction

      void setOpenAction(String name)
    • setOpenAction

      void setOpenAction(PdfAction action)
    • addAdditionalAction

      void addAdditionalAction(PdfName actionType, PdfAction action)
    • setPageLabels

      void setPageLabels(PdfPageLabels pageLabels)
    • addJavaScript

      void addJavaScript(PdfAction js)
    • setCropBoxSize

      void setCropBoxSize(Rectangle crop)
    • setBoxSize

      void setBoxSize(String boxName, Rectangle size)
    • addCalculationOrder

      void addCalculationOrder(PdfFormField formField) throws DocumentException
      Throws:
      DocumentException
    • setSigFlags

      void setSigFlags(int f) throws DocumentException
      Throws:
      DocumentException
    • addFormFieldRaw

      void addFormFieldRaw(PdfFormField field)
    • addAnnotation

      void addAnnotation(PdfAnnotation annot)
    • setDuration

      void setDuration(int seconds)
      Sets the display duration for the page (for presentations)
      Parameters:
      seconds - the number of seconds to display the page
    • setTransition

      void setTransition(PdfTransition transition)
      Sets the transition for the page
      Parameters:
      transition - the PdfTransition object
    • setPageAction

      void setPageAction(PdfName actionType, PdfAction action)
    • isStrictImageSequence

      boolean isStrictImageSequence()
      Getter for property strictImageSequence.
      Returns:
      Value of property strictImageSequence.
    • setStrictImageSequence

      void setStrictImageSequence(boolean strictImageSequence)
      Setter for property strictImageSequence.
      Parameters:
      strictImageSequence - New value of property strictImageSequence.
    • setPageEmpty

      void setPageEmpty(boolean pageEmpty)
    • getDocumentJavaScript

      ArrayList getDocumentJavaScript()
      Method added by Pelikan Stephan
      See Also:
    • setMarginMirroring

      public boolean setMarginMirroring(boolean MarginMirroring)
      Description copied from class: Document
      Set the margin mirroring. It will mirror margins for odd/even pages.

      Note: it will not work with Table.

      Specified by:
      setMarginMirroring in interface DocListener
      Overrides:
      setMarginMirroring in class Document
      Parameters:
      MarginMirroring - true to mirror the margins
      Returns:
      always true
      See Also:
    • convertAnnotation

      static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot) throws IOException
      Throws:
      IOException
    • getMarkPoint

      int getMarkPoint()
      Returns:
      an XmpMetadata byte array
    • incMarkPoint

      void incMarkPoint()