Class PdfDocument
java.lang.Object
com.codename1.pdf.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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddBookmark(String title, PdfPage page, float y) Adds a top-level outline entry that jumps toypoints from the top ofpage.addPage(float width, float height) Adds a page ofwidthbyheightpoints.addPage(PdfPageSize size) Adds a page ofsizeat the end of the document.getPage(int index) Pageindex, from zero.intThe number of pages so far.voidsave(OutputStream out) Writes the document toout.Sets the author metadata.setCompress(boolean compress) Turns stream compression off, for a file that can be read in a text editor while debugging.setCreationDate(long millis) Sets the creation date, in milliseconds since the epoch; defaults to the time the document object was made.setCreator(String creator) Sets the name of the application that created the content.setKeywords(String keywords) Sets the keywords metadata.setSubject(String subject) Sets the subject metadata.Sets the document title readers show in place of the file name.byte[]Returns the document as the bytes of a PDF file.
-
Constructor Details
-
PdfDocument
public PdfDocument()
-
-
Method Details
-
addPage
Adds a page ofsizeat the end of the document. -
addPage
Adds a page ofwidthbyheightpoints. -
getPageCount
public int getPageCount()The number of pages so far. -
getPage
Pageindex, from zero. -
addBookmark
Adds a top-level outline entry that jumps toypoints from the top ofpage. -
setTitle
Sets the document title readers show in place of the file name. -
setAuthor
Sets the author metadata. -
setSubject
Sets the subject metadata. -
setKeywords
Sets the keywords metadata. -
setCreator
Sets the name of the application that created the content. -
setCreationDate
Sets the creation date, in milliseconds since the epoch; defaults to the time the document object was made. -
setCompress
Turns stream compression off, for a file that can be read in a text editor while debugging. On by default. -
save
Writes the document toout. 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
Returns the document as the bytes of a PDF file.- Throws:
IOException
-