Class PdfExport

java.lang.Object
com.codename1.wordprocessing.PdfExport

public final class PdfExport extends Object

Writes a laid out document as PDF, page for page as DocumentView shows it.

DocumentModel model = new DocumentModel(new Document());
model.insertBlock(0, new Paragraph(model.newId('p'), RichText.of("Quarterly review")));
PdfMeasurer fonts = new PdfMeasurer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
    PdfExport.write(new DocumentLayout(model.getDocument(), fonts), fonts, out);
    Log.p("PDF bytes: " + out.size());
} catch (IOException e) {
    Log.e(e);
}

Links become link annotations, tracked insertions are underlined and deletions struck through, and an embedded component is drawn from its snapshot, or as an empty frame when it has none. Pictures other than PNG and JPEG are drawn as frames.

  • Method Details

    • write

      public static void write(Document doc, OutputStream out) throws IOException

      Lays a document out with the standard PDF fonts and writes it.

      Parameters
      • doc: the document

      • out: the stream; it is flushed but not closed

      Throws
      • IOException: when the stream fails
      Throws:
      IOException
    • write

      public static void write(DocumentLayout layout, PdfMeasurer fonts, OutputStream out) throws IOException

      Writes a layout.

      Parameters
      • layout: the layout; for the text to fit its lines it should have been made with fonts

      • fonts: the fonts text is drawn in

      • out: the stream; it is flushed but not closed

      Throws
      • IOException: when the stream fails
      Throws:
      IOException
    • toPdf

      public static PdfDocument toPdf(DocumentLayout layout, PdfMeasurer fonts)

      Draws a layout into a new PDF document, for callers that add metadata or pages of their own before saving.

      Parameters
      • layout: the layout

      • fonts: the fonts text is drawn in

      Returns

      the PDF document

    • drawItems

      public static void drawItems(PdfPage page, PdfCanvas c, List<LayoutItem> items, PdfMeasurer fonts, Map<String,PdfImage> images)

      Draws laid out items on a PDF canvas, the way a page of a document is drawn. Callers that place text of their own on a page -- a slide's text boxes -- translate the canvas to the box and call this.

      Parameters
      • page: the page, which receives the links of linked text

      • c: the canvas

      • items: the items, in points

      • fonts: the fonts the items were measured with

      • images: decoded images by picture, shared across calls so each is embedded once