Class PdfLayout

java.lang.Object
com.codename1.pdf.PdfLayout

public final class PdfLayout extends Object

Flows paragraphs, tables, images and spacing down the pages of a PdfDocument, adding pages as they fill up.

String longText = "Revenue grew in every region this quarter.";
PdfDocument doc = new PdfDocument();
PdfLayout layout = new PdfLayout(doc, PdfPageSize.A4, 54);
layout.setPageDecorator(new PdfLayout.PageDecorator() {
    public void decorate(PdfPage page, int number, int count) {
        page.getCanvas().setFont(PdfFont.HELVETICA, 9)
                .drawText("Page " + number + " of " + count, 54, page.getHeight() - 30);
    }
});
layout.add(new PdfParagraph("Quarterly report", PdfFont.HELVETICA_BOLD, 20));
layout.add(new PdfParagraph(longText, PdfFont.TIMES_ROMAN, 11)
        .setAlignment(PdfParagraph.ALIGN_JUSTIFY));
layout.finish();
byte[] pdf = doc.toByteArray();

The layout draws through the pages' ordinary canvases, so free drawing can be mixed in: getCurrentPage() and getY() say where the flow stands.

  • Field Details

    • ALIGN_LEFT

      public static final int ALIGN_LEFT
      Images are placed against the left margin.
      See Also:
    • ALIGN_CENTER

      public static final int ALIGN_CENTER
      Images are centered.
      See Also:
    • ALIGN_RIGHT

      public static final int ALIGN_RIGHT
      Images are placed against the right margin.
      See Also:
  • Constructor Details

    • PdfLayout

      public PdfLayout(PdfDocument document, PdfPageSize size, float margin)
      A layout adding pages of size to document, with margin points on every side.
  • Method Details

    • setMargins

      public PdfLayout setMargins(float top, float right, float bottom, float left)
      Sets the four margins in points. Applies from the next page.
    • setPageDecorator

      public PdfLayout setPageDecorator(PdfLayout.PageDecorator decorator)
      Sets the decorator called for every page by finish().
    • getCurrentPage

      public PdfPage getCurrentPage()
      The page the flow is on, starting one if there is none yet.
    • getY

      public float getY()
      The distance from the top of the current page to where the next element starts.
    • getContentWidth

      public float getContentWidth()
      The width between the side margins.
    • addSpace

      public PdfLayout addSpace(float points)
      Adds vertical space. Space that runs past the bottom margin ends the page and is not carried over.
    • addPageBreak

      public PdfLayout addPageBreak()
      Continues the flow at the top of a new page.
    • addRule

      public PdfLayout addRule(float lineWidth, int rgb)
      Adds a horizontal line across the content width.
    • addImage

      public PdfLayout addImage(PdfImage image, float width, int alignment)
      Adds image width points wide, keeping its aspect ratio; a width wider than the content is reduced to fit, and so is an image taller than a page.
    • add

      public PdfLayout add(PdfParagraph paragraph)
      Adds a paragraph, continuing it on new pages as needed.
    • add

      public PdfLayout add(PdfTable table)
      Adds a table, splitting it across pages as needed.
    • finish

      public void finish()
      Runs the page decorator over every page this layout added. Call it once, after the last element.