Document document
)
{
// 1. Instantiate the stamper!
/* NOTE: The PageStamper is optimized for dealing with pages. */
PageStamper stamper = new PageStamper();
// 2. Numbering each page...
StandardType1Font font = new StandardType1Font(
document,
StandardType1Font.FamilyEnum.Courier,
true,
false
);
DeviceRGBColor redColor = new DeviceRGBColor(1, 0, 0);
int margin = 32;
for(Page page : document.getPages())
{
// 2.1. Associate the page to the stamper!
stamper.setPage(page);
// 2.2. Stamping the page number on the foreground...
{
PrimitiveComposer foreground = stamper.getForeground();
foreground.setFont(font,16);
foreground.setFillColor(redColor);
Dimension2D pageSize = page.getSize();
int pageNumber = page.getIndex() + 1;
boolean pageIsEven = (pageNumber % 2 == 0);
foreground.showText(
Integer.toString(pageNumber),
new Point2D.Double(
(pageIsEven
? margin
: pageSize.getWidth() - margin),
pageSize.getHeight() - margin
),
(pageIsEven
? AlignmentXEnum.Left
: AlignmentXEnum.Right),
AlignmentYEnum.Bottom,
0
);
}
// 2.3. End the stamping!
stamper.flush();
}
}