Class PdfDocument

java.lang.Object
com.codename1.pdf.PdfDocument

public final class PdfDocument extends Object

Builds a PDF file. Works the same in an application and on a Codename One backend server: nothing here needs a display.

PdfDocument doc = new PdfDocument();
doc.setTitle("Invoice 2031");
PdfPage page = doc.addPage(PdfPageSize.A4);
PdfCanvas c = page.getCanvas();
c.setFont(PdfFont.HELVETICA_BOLD, 20);
c.drawText("Invoice 2031", 72, 96);
byte[] pdf = doc.toByteArray();

For flowing text, tables and automatic page breaks use PdfLayout on top of a document. A document is not safe for use by several threads at once, but separate documents are independent.

  • Constructor Details

    • PdfDocument

      public PdfDocument()
  • Method Details

    • addPage

      public PdfPage addPage(PdfPageSize size)
      Adds a page of size at the end of the document.
    • addPage

      public PdfPage addPage(float width, float height)
      Adds a page of width by height points.
    • getPageCount

      public int getPageCount()
      The number of pages so far.
    • getPage

      public PdfPage getPage(int index)
      Page index, from zero.
    • addBookmark

      public PdfBookmark addBookmark(String title, PdfPage page, float y)
      Adds a top-level outline entry that jumps to y points from the top of page.
    • setTitle

      public PdfDocument setTitle(String title)
      Sets the document title readers show in place of the file name.
    • setAuthor

      public PdfDocument setAuthor(String author)
      Sets the author metadata.
    • setSubject

      public PdfDocument setSubject(String subject)
      Sets the subject metadata.
    • setKeywords

      public PdfDocument setKeywords(String keywords)
      Sets the keywords metadata.
    • setCreator

      public PdfDocument setCreator(String creator)
      Sets the name of the application that created the content.
    • setCreationDate

      public PdfDocument setCreationDate(long millis)
      Sets the creation date, in milliseconds since the epoch; defaults to the time the document object was made.
    • sign

      public PdfDocument sign(PdfSignature signature)
      Signs the document with signature when it is saved. Pass null to save unsigned again. Every save signs afresh, so the bytes differ between saves.
      Throws:
      IllegalArgumentException - if a visible signature is placed on a page of another document
    • setCompress

      public PdfDocument setCompress(boolean compress)
      Turns stream compression off, for a file that can be read in a text editor while debugging. On by default.
    • save

      public void save(OutputStream out) throws IOException
      Writes the document to out. The stream is flushed, not closed. A document with no pages gets one empty page, since a PDF must have at least one.
      Throws:
      IOException
    • toByteArray

      public byte[] toByteArray() throws IOException
      Returns the document as the bytes of a PDF file.
      Throws:
      IOException