Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public final class PdfLayout
- Object
- 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 types
interface PdfLayout.PageDecorator | Draws the parts of a page outside the flow: running headers, footers, page numbers. |
Fields
public static final int ALIGN_LEFT = 0 | Images are placed against the left margin. |
public static final int ALIGN_CENTER = 1 | Images are centered. |
public static final int ALIGN_RIGHT = 2 | Images are placed against the right margin. |
Constructors
public PdfLayout(PdfDocument document, PdfPageSize size, float margin) | A layout adding pages of size to document, with margin points on every side. |
Methods
Inherited methods
Field details
ALIGN_LEFT
public static final int ALIGN_LEFT = 0Images are placed against the left margin.
ALIGN_CENTER
public static final int ALIGN_CENTER = 1Images are centered.
ALIGN_RIGHT
public static final int ALIGN_RIGHT = 2Images are placed against the right margin.
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.