Class PdfDocument

  • All Implemented Interfaces:
    DocListener, ElementListener, java.util.EventListener

    class PdfDocument
    extends Document
    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.

    Since:
    2.0.8 (class was package-private before)
    See Also:
    Document, DocListener, PdfWriter
    • Field Detail

      • writer

        private PdfWriter writer
        The PdfWriter.
      • 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.
      • leading

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

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

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

        protected boolean isSectionTitle
        Signals that onParagraph is valid (to avoid that a Chapter/Section title is treated as a Paragraph).
        Since:
        2.1.2
      • leadingCount

        protected int leadingCount
        Signals that the current leading has to be subtracted from a YMark object when positive.
        Since:
        2.1.2
      • anchorAction

        protected PdfAction anchorAction
        The current active PdfAction when processing an Anchor.
      • textEmptySize

        private int textEmptySize
      • xmpMetadata

        private byte[] xmpMetadata
        XMP Metadata for the page.
      • nextMarginLeft

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

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

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

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

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

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

        private java.util.ArrayList lines
        The lines that are written until now.
      • lastElementType

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

        private static final java.lang.String hangingPunctuation
        The characters to be applied the hanging punctuation.
        See Also:
        Constant Field Values
      • 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.
      • viewerPreferences

        private final PdfViewerPreferencesImp viewerPreferences
        Contains the Viewer preferences of this PDF document.
      • localDestinations

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

        private int jsCounter
        Stores a list of document level JavaScript actions.
      • documentLevelJS

        private final java.util.HashMap documentLevelJS
      • SIXTEEN_DIGITS

        private static final java.text.DecimalFormat SIXTEEN_DIGITS
      • documentFileAttachment

        private final java.util.HashMap documentFileAttachment
      • openActionName

        private java.lang.String openActionName
      • openActionAction

        private PdfAction openActionAction
      • markPoint

        private int markPoint
      • nextPageSize

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

        private java.util.HashMap thisBoxSize
        This is the size of the several boxes of the current Page.
      • boxSize

        private final java.util.HashMap boxSize
        This is the size of the several boxes that will be used in the next page.
      • pageEmpty

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

        private int duration
        The duration of the page
      • transition

        private PdfTransition transition
        The page transition
      • pageResources

        private PageResources pageResources
        This are the page resources of the current Page.
      • strictImageSequence

        private boolean strictImageSequence
        Holds value of property strictImageSequence.
      • imageEnd

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

        private Image imageWait
        This is the image that could not be shown on a previous page.
    • Constructor Detail

      • PdfDocument

        PdfDocument()
      • PdfDocument

        PdfDocument​(java.util.Calendar globalDate)
        Constructs a new PDF document.
        Parameters:
        globalDate -
    • Method Detail

      • addWriter

        void addWriter​(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
      • getLeading

        public float getLeading()
        Getter for the current leading.
        Returns:
        the current leading
        Since:
        2.1.2
      • setLeading

        void setLeading​(float leading)
        Setter for the current leading.
        Parameters:
        leading - the current leading
        Since:
        2.1.6
      • 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
      • 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
      • 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
      • setXmpMetadata

        public void setXmpMetadata​(byte[] xmpMetadata)
        Use this method to set the XMP Metadata.
        Parameters:
        xmpMetadata - The xmpMetadata to set.
      • newPage

        public boolean newPage()
        Makes a new page and sends it to the PdfWriter.
        Specified by:
        newPage in interface DocListener
        Overrides:
        newPage in class Document
        Returns:
        a boolean
      • 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
      • 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
      • 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
      • carriageReturn

        private void carriageReturn()
        If the current line is not empty or null, it is added to the arraylist of lines and a new empty line is added.
      • 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.
      • flushLines

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

        void writeLineToContent​(PdfLine line,
                                PdfContentByte text,
                                PdfContentByte graphics,
                                java.lang.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
      • 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
      • addSpacing

        protected void addSpacing​(float extraspace,
                                  float oldleading,
                                  Font f)
        Adds extra space. This method should probably be rewritten.
      • addOutline

        void addOutline​(PdfOutline outline,
                        java.lang.String name)
        Adds a named outline to the document .
        Parameters:
        outline - the outline to be added
        name - the name of this local destination
      • 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
      • calculateOutlineCount

        private void calculateOutlineCount()
        Updates the count in the outlines.
      • traverseOutlineCount

        private void traverseOutlineCount​(PdfOutline outline)
        Recursive method to update the count in the outlines.
      • writeOutlines

        private void writeOutlines()
                            throws java.io.IOException
        Writes the outline tree to the body of the PDF document.
        Throws:
        java.io.IOException
      • outlineTree

        private void outlineTree​(PdfOutline outline)
                          throws java.io.IOException
        Recursive method used to write outlines.
        Throws:
        java.io.IOException
      • setPageLabels

        void setPageLabels​(PdfPageLabels pageLabels)
        Sets the page labels
        Parameters:
        pageLabels - the page labels
      • localGoto

        private void localGoto​(java.lang.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
      • remoteGoto

        private void remoteGoto​(java.lang.String filename,
                                java.lang.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

        private void remoteGoto​(java.lang.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
      • 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
      • getLocalGotoAction

        private PdfAction getLocalGotoAction​(java.lang.String name)
      • localDestination

        private boolean localDestination​(java.lang.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
      • addJavaScript

        void addJavaScript​(PdfAction js)
      • addJavaScript

        void addJavaScript​(java.lang.String name,
                           PdfAction js)
      • getDocumentLevelJS

        java.util.HashMap getDocumentLevelJS()
      • addFileAttachment

        void addFileAttachment​(java.lang.String description,
                               PdfFileSpecification fs)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • getDocumentFileAttachment

        java.util.HashMap getDocumentFileAttachment()
      • setOpenAction

        void setOpenAction​(java.lang.String name)
      • setOpenAction

        void setOpenAction​(PdfAction action)
      • addAdditionalAction

        void addAdditionalAction​(PdfName actionType,
                                 PdfAction action)
      • setCollection

        public void setCollection​(PdfCollection collection)
        Sets the collection dictionary.
        Parameters:
        collection - a dictionary of type PdfCollection
      • getAcroForm

        PdfAcroForm getAcroForm()
        Gets the AcroForm object.
        Returns:
        the PdfAcroform object of the PdfDocument
      • setSigFlags

        void setSigFlags​(int f)
      • addCalculationOrder

        void addCalculationOrder​(PdfFormField formField)
      • getMarkPoint

        int getMarkPoint()
      • incMarkPoint

        void incMarkPoint()
      • setCropBoxSize

        void setCropBoxSize​(Rectangle crop)
      • setBoxSize

        private void setBoxSize​(java.lang.String boxName,
                                Rectangle size)
      • setNewPageSizeAndMargins

        private void setNewPageSizeAndMargins()
      • getBoxSize

        Rectangle getBoxSize​(java.lang.String boxName)
        Gives the size of a trim, art, crop or bleed box, or null if not defined.
        Parameters:
        boxName - crop, trim, art or bleed
      • setPageEmpty

        void setPageEmpty​(boolean pageEmpty)
      • 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
      • 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.
      • clearTextWrap

        public void clearTextWrap()
        Method added by Pelikan Stephan
      • fitsPage

        private boolean fitsPage​(PdfPTable table,
                                 float margin)
        Checks if a PdfPTable fits the current page of the PdfDocument.
        Parameters:
        table - the table that has to be checked
        margin - a certain margin
        Returns:
        true if the PdfPTable fits the page, false otherwise.
      • addPdfTable

        private void addPdfTable​(Table t)
                          throws DocumentException
        Adds a new table to the document.
        Parameters:
        t - Table to add. Rendered rows will be deleted after processing.
        Throws:
        DocumentException
        Since:
        iText 2.0.8
      • mayBeRemoved

        private boolean mayBeRemoved​(java.util.ArrayList row)
      • bottom

        float bottom​(Table table)
        Returns the bottomvalue of a Table if it were added to this document.
        Parameters:
        table - the table that may or may not be added to this document
        Returns:
        a bottom value