I did it again, Header, footer and image in itext!
Hello, for those who are interested in adding a header and footer in a pdf file using itext, the solution i found its quite simple, just add this servlet to your project like any other but dont make aservlet call to it:
/*
* $Id: EndPage.java,v 1.3 2005/05/09 11:52:50 blowagie Exp $
* $Name: $
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
/**
* Demonstrates the use of PageEvents.
*/
public class EndPage extends PdfPageEventHelper {
/**
* Demonstrates the use of PageEvents.
* @param args no arguments needed
*/
public static void main(String[] args)
{
Document document = new Document(PageSize.A4, 50, 50, 70, 70);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("endpage.pdf"));
writer.setPageEvent(new EndPage());
document.open();
String text = "Lots of text. ";
for (int k = 0; k < 10; ++k)
text += text;
document.add(new Paragraph(text));
document.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
/**
* @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWrite r, com.lowagie.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
try {
/*Image jpg = Image.getInstance("logo.jpg");
jpg.setAlignment(Image.RIGHT);
jpg.scaleAbsolute(100, 30);*/
Rectangle page = document.getPageSize();
PdfPTable head = new PdfPTable(1);
PdfPCell cell1 = new PdfPCell(new Phrase("Pueblo Nativo S.A. de C.V", FontFactory.getFont(FontFactory.HELVETICA, 17f)));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setBorder(0);
for (int k = 1; k <= 1; ++k)
head.addCell(cell1);
//head.addCell(new Phrase("Pueblo Nativo S.A. de C.V", FontFactory.getFont(FontFactory.HELVETICA, 16f)));
head.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
head.writeSelectedRows(0, -1, document.leftMargin(), page.height() - document.topMargin() + head.getTotalHeight(),
writer.getDirectContent());
PdfPTable foot = new PdfPTable(1);
PdfPCell cell = new PdfPCell("This place is to wirte your footer text "));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorder(0);
for (int k = 1; k <= 1; ++k)
foot.addCell(cell);
//foot.addCell("This place is to write your footer text ");
foot.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
writer.getDirectContent());
}
catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
After doing the servlet, in your original pdf in where u generate the report u want, just add this line code to call it:
writer.setPageEvent(new EndPage());
where EndPage is the name of the java class for the file, after doing this if u want to add an image to the header or footer just chage a part of code like this:
PdfPTable foot = new PdfPTable(1);
Image jpg = Image.getInstance("logo.jpg");
jpg.setAlignment(Image.RIGHT);
jpg.scaleAbsolute(100, 30);
PdfPCell cell = new PdfPCell("This place is to wirte your footer text "));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorder(0);
for (int k = 1; k <= 1; ++k)
foot.addCell(cell);
foot.addCell(jpg);
foot.addCell("This place is to write your footer text ");
foot.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
writer.getDirectContent());
If u have any doubts, just post again, and i hope this helps!
Beltazor
By thw way, i found on the net a code in java that i adapted to creator to get a quantity wich is float or string and convert it to its correspondent in usual languaje like $23.2 to twenty three 23/100 but this is ONLY in spanish, so if u want this code too, just post again.

