Class PdfLayout
java.lang.Object
com.codename1.pdf.PdfLayout
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceDraws the parts of a page outside the flow: running headers, footers, page numbers. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intImages are centered.static final intImages are placed against the left margin.static final intImages are placed against the right margin. -
Constructor Summary
ConstructorsConstructorDescriptionPdfLayout(PdfDocument document, PdfPageSize size, float margin) A layout adding pages ofsizetodocument, withmarginpoints on every side. -
Method Summary
Modifier and TypeMethodDescriptionadd(PdfParagraph paragraph) Adds a paragraph, continuing it on new pages as needed.Adds a table, splitting it across pages as needed.Addsimagewidthpoints wide, keeping its aspect ratio; a width wider than the content is reduced to fit, and so is an image taller than a page.Continues the flow at the top of a new page.addRule(float lineWidth, int rgb) Adds a horizontal line across the content width.addSpace(float points) Adds vertical space.voidfinish()Runs the page decorator over every page this layout added.floatThe width between the side margins.The page the flow is on, starting one if there is none yet.floatgetY()The distance from the top of the current page to where the next element starts.setMargins(float top, float right, float bottom, float left) Sets the four margins in points.setPageDecorator(PdfLayout.PageDecorator decorator) Sets the decorator called for every page byfinish().
-
Field Details
-
ALIGN_LEFT
public static final int ALIGN_LEFTImages are placed against the left margin.- See Also:
-
ALIGN_CENTER
public static final int ALIGN_CENTERImages are centered.- See Also:
-
ALIGN_RIGHT
public static final int ALIGN_RIGHTImages are placed against the right margin.- See Also:
-
-
Constructor Details
-
PdfLayout
A layout adding pages ofsizetodocument, withmarginpoints on every side.
-
-
Method Details
-
setMargins
Sets the four margins in points. Applies from the next page. -
setPageDecorator
Sets the decorator called for every page byfinish(). -
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
Adds vertical space. Space that runs past the bottom margin ends the page and is not carried over. -
addPageBreak
Continues the flow at the top of a new page. -
addRule
Adds a horizontal line across the content width. -
addImage
-
add
Adds a paragraph, continuing it on new pages as needed. -
add
-
finish
public void finish()Runs the page decorator over every page this layout added. Call it once, after the last element.
-