Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class DocumentLayout

  1. Object
  2. DocumentLayout

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.

Constructors

public DocumentLayout(Document doc, Measurer measurer)Lays a document out.

Methods

public PageSpec getPageSpec()The page geometry used.
public int getPageCount()The number of pages; at least one.
public LayoutPage getPage(int index)A page.
public Measurer getMeasurer()The measurer this layout was made with.
public TextPosition positionAt(int page, float x, float y)The caret position nearest a point on a page.
public float[] caretRect(TextPosition position)Where the caret for a position is drawn.

Inherited methods

Constructor details

DocumentLayout

public DocumentLayout(Document doc, Measurer measurer)
Lays a document out.

Parameters

doc Document
the document
measurer 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 int
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 int
the page index
x float
from the page’s left edge, in points
y float
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 TextPosition
the position

Returns

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