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.

[5194 byte] By [Beltazora] at [2007-11-26 14:45:33]
# 1

Could you tell me, how can I generatae pdf out of a large table?

I think of an approach. Have the whole table into Graphics2D. Then take clips on subsequent offsets and add as templates to the PdfContentByte. In order to achive could you tell me how can I create the looping. How can I get the max "height" n "width" to iterate.

Thanks

chandrajeeta at 2007-7-8 8:33:10 > top of Java-index,Development Tools,Java Tools...
# 2

To generate a large table it is quite simple, well in the way i did it. Just dynamically get the info from the table, at same time in pdf servlet create a table indicating cells and rows and use the table.addcell(element); to add a cell. I don't know why do you want the dimensions of the cell but you can set it using this example code:

/*

* $Id: CellHeights.java,v 1.5 2006/09/14 22:52:48 xlv 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

*/

package com.lowagie.examples.objects.tables;

import java.io.FileOutputStream;

import com.lowagie.text.Document;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfPCell;

import com.lowagie.text.pdf.PdfPTable;

import com.lowagie.text.pdf.PdfWriter;

/**

* Change the height of a cell (fixed height), disable text wrapping, set a minimum height.

*/

public class CellHeights {

/**

* Height manipulations of cells.

*

* @param args

*no arguments needed

*/

public static void main(String[] args) {

System.out.println("Height");

// step1

Document document = new Document(PageSize.A4);

try {

// step2

PdfWriter writer = PdfWriter.getInstance(document,

new FileOutputStream("CellHeights.pdf"));

// step3

document.open();

// step4

PdfPTable table = new PdfPTable(2);

table.setExtendLastRow(true);

PdfPCell cell;

// wrap / nowrap

cell = new PdfPCell(new Paragraph("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah"));

table.addCell("wrap");

cell.setNoWrap(false);

table.addCell(cell);

table.addCell("no wrap");

cell.setNoWrap(true);

table.addCell(cell);

// height

cell = new PdfPCell(new Paragraph("1. blah blah\n2. blah blah blah\n3. blah blah\n4. blah blah blah\n5. blah blah\n6. blah blah blah\n7. blah blah\n8. blah blah blah"));

table.addCell("height");

table.addCell(cell);

table.addCell("fixed height");

cell.setFixedHeight(50f);

table.addCell(cell);

table.addCell("minimum height");

cell = new PdfPCell(new Paragraph("x"));

cell.setMinimumHeight(50f);

table.addCell(cell);

table.addCell("extend last row");

cell = new PdfPCell(new Paragraph("almost no content, but the row is extended"));

table.addCell(cell);

document.add(table);

} catch (Exception de) {

de.printStackTrace();

}

// step5

document.close();

}

}

if u have more questions just post again

Belthazor

Beltazora at 2007-7-8 8:33:10 > top of Java-index,Development Tools,Java Tools...