Reputation: 2068
My question guys is how do I add a footer with multiple lines using iText 1.5.4 I know my question is similar to this one but that question till now is not answered. Also I dont require the solution to use tables in a footer.
Basically what I want to achieve is something like this
Upvotes: 0
Views: 6913
Reputation: 677
I struggled a lot with multiline header and footer in itext 5. Ultimately I switched to itext 7 and the implementation and examples made it very straight-forward. If upgrading is an option, I would just suggest using the latest version if you run into this problem.
Upvotes: 0
Reputation: 2068
I achieved the layout I wanted similar to the image I posted with my question using the code below. Im not that familiar with itext so if anybody has a better way of doing this your comments and suggestions will be very much appreciated. Thanks!
public static void main(String[] args) throws DocumentException, IOException{
Document document = new Document(PageSize.A4, 50, 50, 70, 70);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("C:/pdfwithfooter.pdf"));
document.open();
document.add(new Paragraph(
"Hello Body of the PDF"));
/** Format Font **/
Font fontFooter = new Font(FontFactory.getFont(
FontFactory.HELVETICA, 6));
/**
* Format Table Cell Borders
*/
// cell.disableBorderSide(Rectangle.BOX);
Rectangle page = document.getPageSize();
PdfPTable foot = new PdfPTable(4);
PdfPCell footCell = new PdfPCell(new Phrase(
"OUR DOCUMENT'S LEGEND", FontFactory.getFont(
FontFactory.HELVETICA, 6, Font.BOLDITALIC)));
footCell.setColspan(2);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase(""));
footCell.setColspan(2);
foot.addCell(footCell);
// 1st row
footCell = new PdfPCell(new Phrase("COX - COX", fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase("EQ - Equipment", fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase("OUT-XYZ - Out XXXXXXXXXXXXX",
fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase(
"ADJ-XX - Adjustment on XXXXXX XXXX", fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
// 2nd row
footCell = new PdfPCell(new Phrase("EB - Electric Bills",
fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase("T-BB - Transfer to Big Boat",
fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase("T-XXXX - Transfer to XXXX",
fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase("GX - Get XXXXXX", fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
// 3rd row
footCell = new PdfPCell(new Phrase(
"EXCEPTIONS - Invalid X Y VALUES", fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase("R XX - Removal of XX",
fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase("T-X - Transfer of XXXX",
fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase(
"T-LX - Transfer of Leased XXXX", fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
// 4th row
footCell = new PdfPCell(new Phrase("LX - Leased XXXX", fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase(
"T-TX - Transfer to TREE XXXXXXX", fontFooter));
footCell.setPaddingLeft(15);
foot.addCell(footCell);
footCell = new PdfPCell(new Phrase(
"Adj-XXX - Adjustment Some Value",
fontFooter));
footCell.setPaddingLeft(15);
footCell.setColspan(2);
foot.addCell(footCell);
foot.setTotalWidth(page.width() - document.leftMargin()
- document.rightMargin());
foot.writeSelectedRows(0, -1, document.leftMargin(),
document.bottomMargin(), writer.getDirectContent());
document.close();
}
Upvotes: 0
Reputation: 8771
I suggest you to follow this example :
http://itextpdf.com/examples/iia.php?id=103
You will be able to add whatever you want as header and footer. I've been using this in the past and was able to put a Table in the footer.
Note this class HeaderFooter extends PdfPageEventHelper
and public void onEndPage(PdfWriter writer, Document document)
in their example.
Hope this helps!
Upvotes: 1