I hope following sample code help you .I used it one year ago.
package org.mycompany.myproject.util;
import com.lowagie.text.pdf.*;
import com.lowagie.text.*;
import java.io.*;
import com.lowagie.text.Font;
import java.awt.Color;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
public class requestUtils {
private String fontTahomaFile;
private PdfPCell cell;
public Boolean CreatePDF(IRequest request, String sfilename, String
fontTahomaFile, ResourceBundle bundle) throws Exception {
this.fontTahomaFile = fontTahomaFile;
try {
Document document = new Document(PageSize.A4, 0, 50, 0, 50);
BaseFont bf;
Font font;
BaseFont bf2;
Font font2;
BaseFont bfsp;
Font fontsp;
bfsp = BaseFont.createFont(fontTahomaFile, BaseFont.IDENTITY_H,
false);
fontsp = new Font(bfsp, 7);
bf = BaseFont.createFont(fontTahomaFile, BaseFont.IDENTITY_H,
false);
font = new Font(bf, 8);
//font.setColor(new Color(225, 225, 204));
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(sfilename));
document.open();
int tableOnpage = 0;
PdfPTable table_h = new PdfPTable(3);
table_h.getDefaultCell().setPadding(2);
table_h.setRunDirection(table_h.ALIGN_RIGHT);
PdfPCell cell_h;
cell_h = new PdfPCell(new Paragraph(" ", font));
cell_h.setBorderColor(new Color(255, 255, 255));
cell_h.setBorderColorLeft(new Color(255, 255, 255));
cell_h.setBorderColorRight(new Color(255, 255, 255));
cell_h.setColspan(3);
table_h.addCell(cell_h);
table_h.setRunDirection(table_h.ALIGN_RIGHT);
cell_h = new PdfPCell(new Paragraph(bundle.getString("header") + " "
+"1388", font));
cell_h.setBackgroundColor(new Color(214, 212, 216));
cell_h.setColspan(3);
cell_h.setRunDirection(cell_h.ALIGN_RIGHT);
cell_h.setHorizontalAlignment(cell_h.ALIGN_CENTER);
table_h.addCell(cell_h);
cell_h = setCell(bundle.getString("name") + ": " + "myname");
table_h.addCell(cell_h);
cell_h = setCell(bundle.getString("ID") + ": " + "1");
table_h.addCell(cell_h);
cell_h = setCell(bundle.getString("lastname") + ": " +
"myLastname");
cell_h.setColspan(2);
table_h.addCell(cell_h);
cell_h = setCell(getDate() + " :" + bundle.getString("rDate"));
cell_h.setRunDirection(cell_h.ALIGN_RIGHT);
table_h.addCell(cell_h);
cell_h = new PdfPCell(new Paragraph(" ", font));
cell_h.setBorderColorTop(new Color(61, 61, 61));
cell_h.setBorderColorBottom(new Color(61, 61, 61));
//cell_h.setBorderColor(new Color(255, 255, 255));
cell_h.setColspan(3);
table_h.addCell(cell_h);
document.add(table_h);
PdfPTable table = new PdfPTable(6);
table.setRunDirection(table.ALIGN_RIGHT);
cell = setCell(bundle.getString("ColumnHeader1"));
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(239, 239, 239));
table.addCell(cell);
cell.setNoWrap(false);
cell = setCell(bundle.getString("ColumnHeader2"));
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(239, 239, 239));
table.addCell(cell);
cell = setCell(bundle.getString("ColumnHeader3"));
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(239, 239, 239));
table.addCell(cell);
cell = setCell(bundle.getString("ColumnHeader4"));
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(239, 239, 239));
table.addCell(cell);
cell = setCell(bundle.getString("ColumnHeader5"));
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(239, 239, 239));
table.addCell(cell);
cell = setCell(bundle.getString("ColumnHeader6"));
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(239, 239, 239));
table.addCell(cell);
cell = new PdfPCell(new Paragraph(" ", font));
cell.setBorderColor(new Color(255, 255, 255));
cell.setBorderColorLeft(new Color(255, 255, 255));
cell.setBorderColorRight(new Color(255, 255, 255));
cell.setColspan(6);
table.addCell(cell_h);
document.add(table);
document.close();
return true;
} catch (Exception ex) {
throw ex;
}
}
private PdfPCell setCell(String par) throws DocumentException,
IOException {
PdfPCell cell = null;
Document document = new Document(PageSize.A4, 0, 50, 0, 50);
BaseFont bf;
Font font;
BaseFont bf2;
Font font2;
BaseFont bfsp;
Font fontsp;
bfsp =
BaseFont.createFont(fontTahomaFile, BaseFont.IDENTITY_H, false);
fontsp =
new Font(bfsp, 7);
bf =
BaseFont.createFont(fontTahomaFile, BaseFont.IDENTITY_H, false);
font =
new Font(bf, 10);
cell =
new PdfPCell(new Paragraph(par, fontsp));
cell.setRunDirection(cell.ALIGN_RIGHT);
cell.setHorizontalAlignment(cell.ALIGN_RIGHT);
return cell;
}
}