Class DocumentLayout

java.lang.Object
com.codename1.wordprocessing.DocumentLayout

public final class DocumentLayout extends Object

A document broken into pages: every line, table, picture and footnote placed, in points.

Layout is pure: it reads a Document and a Measurer and touches no user interface, so the same result paints DocumentView, is written by the PDF export, and can run on a server.

DocumentModel model = new DocumentModel(new Document());
model.insertBlock(0, new Paragraph(model.newId('p'), RichText.of("Quarterly review")));
DocumentLayout layout = new DocumentLayout(model.getDocument(), new PdfMeasurer());
for (int i = 0; i < layout.getPageCount(); i++) {
    for (LayoutItem item : layout.getPage(i).getItems()) {
        Log.p("page " + i + ": item at " + item.x + ", " + item.y);
    }
}

Paragraphs break at spaces (a word wider than the line breaks between letters) and honor alignment, justification, indents, spacing, line spacing, page breaks and keep-with-next. List items get bullets or numbers, restarted where the paragraph says so. Table rows move to the next page whole, repeating the header rows; a row taller than a page is split. Footnotes are placed at the bottom of the page that references them and endnotes after the text. PAGE and NUMPAGES fields show the real page number and count.

  • Constructor Details

    • DocumentLayout

      public DocumentLayout(Document doc, Measurer measurer)

      Lays a document out.

      Parameters
      • doc: the document

      • measurer: measures text; use the same one for everything that must match

  • Method Details

    • getPageSpec

      public PageSpec getPageSpec()

      The page geometry used.

      Returns

      the document's page, or A4 when it has none

    • getPageCount

      public int getPageCount()

      The number of pages; at least one.

      Returns

      the page count

    • getPage

      public LayoutPage getPage(int index)

      A page.

      Parameters
      • index: from zero
      Returns

      the page

    • getMeasurer

      public Measurer getMeasurer()

      The measurer this layout was made with.

      Returns

      the measurer

    • positionAt

      public TextPosition positionAt(int page, float x, float y)

      The caret position nearest a point on a page.

      Parameters
      • page: the page index

      • x: from the page's left edge, in points

      • y: from the page's top edge, in points

      Returns

      the position, or null when the page holds no text

    • caretRect

      public float[] caretRect(TextPosition position)

      Where the caret for a position is drawn.

      Parameters
      • position: the position
      Returns

      {page, x, top, height} in points, or null when the paragraph is not laid out