Class Document

java.lang.Object
org.openpdf.text.Document
All Implemented Interfaces:
AutoCloseable, EventListener, DocListener, ElementListener
Direct Known Subclasses:
PdfDocument

public class Document extends Object implements DocListener
A generic Document class.

All kinds of Text-elements can be added to a HTMLDocument. The Document signals all the listeners when an element has been added.

Remark:

  1. Once a document is created you can add some meta information.
  2. You can also set the headers/footers.
  3. You have to open the document before you can write content.
  4. You can only write content (no more meta-formation!) once a document is opened.
  5. When you change the header/footer on a certain page, this will be effective starting on the next page.
  6. After closing the document, every listener (as well as its OutputStream) is closed too.
Example:
// creation of the document with a certain size and certain margins
 Document document = new Document(new Rectangle(595, 842), 50, 50, 50, 50);
  try {
 // creation of the different writers HtmlWriter.getInstance(document , System.out);
 PdfWriter.getInstance(document , new FileOutputStream("text.pdf")); // we add some meta information
 to the document
 document.addAuthor("Bruno Lowagie"); 
 document.addSubject("This is the result of a Test."); 
 // we open the document for writing
 document.open(); 
 document.add(new Paragraph("Hello world"));
 } catch(DocumentException de) { System.err.println(de.getMessage()); }
 document.close();
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
    Has the document already been closed?
    static boolean
    Allows the pdf documents to be produced without compression for debugging purposes.
    protected float
    margin in y direction starting from the bottom
    protected float
    margin in x direction starting from the left
    protected float
    margin in x direction starting from the right
    protected float
    margin in y direction starting from the top
    protected boolean
    Is the document open or not?
    protected int
    Current pagenumber
    protected Rectangle
    The size of the page.
    static boolean
    When true the file access is not done through a memory mapped file.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new Document -object with the A4 page size.
    Document(Rectangle pageSize)
    Constructs a new Document -object using the given rectangle as page size and default margin of 36.
    Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)
    Constructs a new Document -object.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(Element element)
    Adds an Element to the Document.
    boolean
    addAuthor(String author)
    Adds the author to a Document.
    boolean
    Adds the current date and time to a Document.
    boolean
    Adds the current date and time to a Document.
    void
    Adds a DocListener to the Document.
    boolean
    Adds the current date and time to a Document.
    boolean
    Adds the producer to a Document.
    boolean
    addProducer(String producer)
    Adds the provided value as the producer to a Document.
    boolean
    addSubject(String subject)
    Adds the subject to a Document.
    boolean
    Adds the title to a Document.
    void
    Closes the document.
    Gets the text rendering options.
    static String
    Gets the iText version.
    boolean
    Checks if the document is open.
    boolean
    Signals to all listeners, that a new page has to be started.
    void
    Opens the document.
    boolean
    setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
    Sets the margins.
    boolean
    Sets the pagesize.
    void
    Sets the text rendering options.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • compress

      public static boolean compress
      Allows the pdf documents to be produced without compression for debugging purposes.
    • plainRandomAccess

      public static boolean plainRandomAccess
      When true the file access is not done through a memory mapped file. Use it if the file is too big to be mapped in your address space.
    • open

      protected boolean open
      Is the document open or not?
    • close

      protected boolean close
      Has the document already been closed?
    • pageSize

      protected Rectangle pageSize
      The size of the page.
    • marginLeft

      protected float marginLeft
      margin in x direction starting from the left
    • marginRight

      protected float marginRight
      margin in x direction starting from the right
    • marginTop

      protected float marginTop
      margin in y direction starting from the top
    • marginBottom

      protected float marginBottom
      margin in y direction starting from the bottom
    • pageN

      protected int pageN
      Current pagenumber
  • Constructor Details

    • Document

      public Document()
      Constructs a new Document -object with the A4 page size.
    • Document

      public Document(Rectangle pageSize)
      Constructs a new Document -object using the given rectangle as page size and default margin of 36.
      Parameters:
      pageSize - the pageSize
    • Document

      public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)
      Constructs a new Document -object.
      Parameters:
      pageSize - the pageSize
      marginLeft - the margin on the left
      marginRight - the margin on the right
      marginTop - the margin on the top
      marginBottom - the margin on the bottom
  • Method Details

    • getVersion

      public static String getVersion()
      Gets the iText version.
      Returns:
      iText version
    • addDocListener

      public void addDocListener(DocListener listener)
      Adds a DocListener to the Document.
      Parameters:
      listener - the new DocListener.
    • add

      public boolean add(Element element) throws DocumentException
      Adds an Element to the Document.
      Specified by:
      add in interface ElementListener
      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.

      Once the document is opened, you can't write any meta-information anymore. If you change the header/footer after opening the document, the change will be effective starting from the second page. 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
    • setPageSize

      public boolean setPageSize(Rectangle pageSize)
      Sets the pagesize.

      This change will be effective starting from the next page. If you want to change the page size of the first page, you need to set it before opening the document.

      Specified by:
      setPageSize in interface DocListener
      Parameters:
      pageSize - the new pagesize
      Returns:
      a boolean
    • setMargins

      public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
      Sets the margins.

      This change will be effective starting from the next page. If you want to change margins on the first page, you need to set it before opening the document.

      A new page is created when you call:

      • Document.open()
      • Document.newPage(), only if the current page has some content, otherwise no new page will be created

      Specified by:
      setMargins in interface DocListener
      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
    • newPage

      public boolean newPage()
      Signals to all listeners, that a new page has to be started. New pages can only be added on already opened and not yet closed documents.
      Specified by:
      newPage in interface DocListener
      Returns:
      true if the page was added, false if not.
    • 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 AutoCloseable
      Specified by:
      close in interface DocListener
    • addTitle

      public boolean addTitle(String title)
      Adds the title to a Document.

      In case of a PDF this will be visible in the document properties panel.

      In case of a HTML file this will be visible as the title meta tag in the HEAD part of the file.

      Shortcut method to call: add(new Meta(Element.TITLE, title))

      Parameters:
      title - the title
      Returns:
      true if successful, false otherwise
    • addSubject

      public boolean addSubject(String subject)
      Adds the subject to a Document.

      In case of a PDF this will be visible in the document properties panel.

      In case of a HTML file this will be visible as the subject meta tag in the HEAD part of the file.

      Parameters:
      subject - the subject
      Returns:
      true if successful, false otherwise
    • addAuthor

      public boolean addAuthor(String author)
      Adds the author to a Document.

      In case of a PDF this will be visible in the document properties panel.

      In case of a HTML file this will be visible as the author meta tag in the HEAD part of the file.

      Parameters:
      author - the name of the author
      Returns:
      true if successful, false otherwise
    • addProducer

      public boolean addProducer()
      Adds the producer to a Document.

      In case of a PDF this will be visible in the document properties panel.

      In case of a HTML file this will be visible as comment in the HEAD part of the file.

      Returns:
      true if successful, false otherwise
    • addProducer

      public boolean addProducer(String producer)
      Adds the provided value as the producer to a Document.

      The default producer is OpenPDF XX.YY.ZZ where XX.YY.ZZ is the version of the OpenPDF library used to produce the document

      In case of a PDF this will be visible in the document properties panel.

      In case of a HTML file this will be visible as comment in the HEAD part of the file.

      Parameters:
      producer - new producer line value
      Returns:
      true if successful, false otherwise
    • addCreationDate

      public boolean addCreationDate()
      Adds the current date and time to a Document.

      In case of a PDF this will be visible in the document properties panel.

      In case of a HTML file this will be visible as comment in the HEAD part of the file.

      Returns:
      true if successful, false otherwise
    • addCreationDate

      public boolean addCreationDate(PdfDate date)
      Adds the current date and time to a Document.
      Returns:
      true if successful, false otherwise
    • addModificationDate

      public boolean addModificationDate(PdfDate date)
      Adds the current date and time to a Document.
      Returns:
      true if successful, false otherwise
    • isOpen

      public boolean isOpen()
      Checks if the document is open.
      Returns:
      true if the document is open
    • getTextRenderingOptions

      public TextRenderingOptions getTextRenderingOptions()
      Gets the text rendering options.
      Returns:
      the text rendering options
    • setTextRenderingOptions

      public void setTextRenderingOptions(TextRenderingOptions textRenderingOptions)
      Sets the text rendering options.
      Parameters:
      textRenderingOptions - the text rendering options