Class Document

  • All Implemented Interfaces:
    IPropertyContainer, java.io.Closeable, java.lang.AutoCloseable

    public class Document
    extends RootElement<Document>
    Document is the default root element when creating a self-sufficient PDF. It mainly operates high-level operations e.g. setting page size and rotation, adding elements, and writing text at specific coordinates. It has no knowledge of the actual PDF concepts and syntax.

    A Document's rendering behavior can be modified by extending DocumentRenderer and setting an instance of this newly created with setRenderer(com.itextpdf.layout.renderer.DocumentRenderer).

    • Constructor Detail

      • Document

        public Document​(PdfDocument pdfDoc)
        Creates a document from a PdfDocument. Initializes the first page with the PdfDocument's current default PageSize.
        Parameters:
        pdfDoc - the in-memory representation of the PDF document
      • Document

        public Document​(PdfDocument pdfDoc,
                        PageSize pageSize)
        Creates a document from a PdfDocument with a manually set PageSize.
        Parameters:
        pdfDoc - the in-memory representation of the PDF document
        pageSize - the page size
      • Document

        public Document​(PdfDocument pdfDoc,
                        PageSize pageSize,
                        boolean immediateFlush)
        Creates a document from a PdfDocument with a manually set PageSize.
        Parameters:
        pdfDoc - the in-memory representation of the PDF document
        pageSize - the page size
        immediateFlush - if true, write pages and page-related instructions to the PdfDocument as soon as possible.
    • Method Detail

      • close

        public void close()
        Closes the document and associated PdfDocument.
      • add

        public Document add​(AreaBreak areaBreak)
        Terminates the current element, usually a page. Sets the next element to be the size specified in the argument.
        Parameters:
        areaBreak - an AreaBreak, optionally with a specified size
        Returns:
        this element
      • add

        public Document add​(IBlockElement element)
        Description copied from class: RootElement
        Adds an element to the root. The element is immediately placed in the contents.
        Overrides:
        add in class RootElement<Document>
        Parameters:
        element - an element with spacial margins, tabbing, and alignment
        Returns:
        this element
        See Also:
        BlockElement
      • getPdfDocument

        public PdfDocument getPdfDocument()
        Gets PDF document.
        Returns:
        the in-memory representation of the PDF document
      • setRenderer

        public void setRenderer​(DocumentRenderer documentRenderer)
        Changes the DocumentRenderer at runtime. Use this to customize the Document's IRenderer behavior.
        Parameters:
        documentRenderer - the DocumentRenderer to set
      • flush

        public void flush()
        Forces all registered renderers (including child element renderers) to flush their contents to the content stream.
      • relayout

        public void relayout()
        Performs an entire recalculation of the document flow, taking into account all its current child elements. May become very resource-intensive for large documents.

        Do not use when you have set RootElement.immediateFlush to true.

      • getLeftMargin

        public float getLeftMargin()
        Gets the left margin, measured in points
        Returns:
        a float containing the left margin value
      • setLeftMargin

        public void setLeftMargin​(float leftMargin)
        Sets the left margin, measured in points
        Parameters:
        leftMargin - a float containing the new left margin value
      • getRightMargin

        public float getRightMargin()
        Gets the right margin, measured in points
        Returns:
        a float containing the right margin value
      • setRightMargin

        public void setRightMargin​(float rightMargin)
        Sets the right margin, measured in points
        Parameters:
        rightMargin - a float containing the new right margin value
      • getTopMargin

        public float getTopMargin()
        Gets the top margin, measured in points
        Returns:
        a float containing the top margin value
      • setTopMargin

        public void setTopMargin​(float topMargin)
        Sets the top margin, measured in points
        Parameters:
        topMargin - a float containing the new top margin value
      • getBottomMargin

        public float getBottomMargin()
        Gets the bottom margin, measured in points
        Returns:
        a float containing the bottom margin value
      • setBottomMargin

        public void setBottomMargin​(float bottomMargin)
        Sets the bottom margin, measured in points
        Parameters:
        bottomMargin - a float containing the new bottom margin value
      • setMargins

        public void setMargins​(float topMargin,
                               float rightMargin,
                               float bottomMargin,
                               float leftMargin)
        Convenience method to set all margins with one method.
        Parameters:
        topMargin - the upper margin
        rightMargin - the right margin
        leftMargin - the left margin
        bottomMargin - the lower margin
      • getPageEffectiveArea

        public Rectangle getPageEffectiveArea​(PageSize pageSize)
        Returns the area that will actually be used to write on the page, given the current margins. Does not have any side effects on the document.
        Parameters:
        pageSize - the size of the page to
        Returns:
        a Rectangle with the required dimensions and origin point
      • getDefaultProperty

        public <T1> T1 getDefaultProperty​(int property)
        Description copied from interface: IPropertyContainer
        Gets the default property from this entity.
        Specified by:
        getDefaultProperty in interface IPropertyContainer
        Overrides:
        getDefaultProperty in class RootElement<Document>
        Type Parameters:
        T1 - the return type associated with the property
        Parameters:
        property - the property to be retrieved
        Returns:
        the default property value. If the default property is not defined, null will be returned
      • checkClosingStatus

        protected void checkClosingStatus()
        Checks whether a method is invoked at the closed document