Skip to content

PageFormat

Ralf Stuckert edited this page Sep 22, 2016 · 2 revisions

Page Format

By default a document is created with format A4 in portrait and zero margins. You may specify any desired format by passing a PageFormat to the document. You may also use a different format for every page.

Initial Page Format

You can decide which page format to use, when you create the document. As already said, if you use the default document constructor, you will have format A4 in portrait and zero margins:

Document document = new Document(); // A4 portrait, no margins

But you may pass a PageFormat to the document:

PageFormat a4_landscape = new PageFormat(Constants.A4, Orientation.Landscape);
Document document = new Document(a4_landscape);

It is recommended to use the page format builder, which provides a fluent API:

PageFormat a5_landscape = PageFormat.with().A5().landscape().margins(10, 50, 0, 30).build();

Using a different Page Format

Sometimes you may want to use different page formats in one document. This demonstrated by the example Landscape.java:

PageFormat a5_landscape = PageFormat.with().A5().landscape().margins(10, 50, 0, 30).build();
PageFormat a4_portrait = PageFormat.with().margins(40, 50, 40, 60).build();

Document document = new Document(a4_portrait);
...
document.add(paragraph2);

document.add(a5_landscape);
document.add(ControlElement.NEWPAGE);
...
document.add(paragraph1);

The PageFormat is an element, that you can add to the document. This new page layout will be applied to the next and all subsequent pages. You do not need to explicitly create a new page; we could change the format right after the start, but it would not be applied to the first but the second and all subsequent pages. But usually, it makes sense to change the format right before you trigger a new page.

The example creates a page A4 portrait, followed by A5 landscape, A4 portrait and A5 landscape again. You just have a look at the landscape.pdf to see the results.

Page Rotation

You may also rotate both page and content using rotation(). See Rotation.java resp. rotation.pdf.

Clone this wiki locally