I am able to use java and itext to create a pdf file with a page header and with body content. You can view the resulting file at a file sharing site by clicking on this link.
How can I alter the code below so that a page footer is also created at the bottom of the page? I want the footer to say "MyCity, MyState, MyPostalCode MyPhoneNumber". I also want to footer to have a line above it just like the header has a line above it in the example link above.
Here is the code that generates the sample PDF in the link above:
public class HeaderFooter {
public static final String RESULT = "C:\\path\\to\\headerfooter.pdf";
public static void main(String[] args) throws SQLException, DocumentException, IOException {
Document document = new Document(PageSize.A4, 36, 36, 54, 36);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph(""));
document.add(Chunk.NEWLINE);
document.add(new Paragraph("First paragraph."));
document.add(new Paragraph("Second paragraph."));
document.add(new Paragraph("Third paragraph."));
document.newPage();
document.close();
PdfReader reader = new PdfReader(baos.toByteArray());
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++) {
getHeader(i, n).writeSelectedRows(0, -1, 34, 803, stamper.getOverContent(i));
}
stamper.close();
reader.close();
}
public static PdfPTable getHeader(int x, int y) {
PdfPTable table = new PdfPTable(2);
table.setTotalWidth(527);
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.BOTTOM);
table.addCell("This is the Document's Title.");
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
return table;
}
}
For anyone who wants to take one minute to get the above code to work on their computer, the maven dependency to add to pom.xml
is:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.5</version>
</dependency>