Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class PdfDocument

  1. Object
  2. PdfDocument

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.

Constructors

public PdfDocument()

Methods

public PdfPage addPage(PdfPageSize size)Adds a page of size at the end of the document.
public PdfPage addPage(float width, float height)Adds a page of width by height points.
public int getPageCount()The number of pages so far.
public PdfPage getPage(int index)Page index, from zero.
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.
public PdfDocument setTitle(String title)Sets the document title readers show in place of the file name.
public PdfDocument setAuthor(String author)Sets the author metadata.
public PdfDocument setSubject(String subject)Sets the subject metadata.
public PdfDocument setKeywords(String keywords)Sets the keywords metadata.
public PdfDocument setCreator(String creator)Sets the name of the application that created the content.
public PdfDocument setCreationDate(long millis)Sets the creation date, in milliseconds since the epoch; defaults to the time the document object was made.
public PdfDocument setCompress(boolean compress)Turns stream compression off, for a file that can be read in a text editor while debugging.
public void save(OutputStream out) throws IOExceptionWrites the document to out.
public byte[] toByteArray() throws IOExceptionReturns the document as the bytes of a PDF file.

Inherited methods

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.

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.

toByteArray

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