Generating PDF Document Using iText

iText is an open source library that allows programmers to create and manipulate PDF. It enhances applications with dynamic PDF document generation or manipulation. iText is available in Java,Dot Net and Android. Download the itext core binary from the web page http://sourceforge.net/projects/itext/Create a new Java project in Eclipse and ass iText library (jar file) to your class path.

import java.io.FileOutputStream;
import java.util.ArrayList;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter
;

public class PdfOutput {
// Folder path to store PDF file
private static String FILE = "f:/sandhya/OUTPUT/First.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
Font.BOLD);
private ArrayList<?> arr = null;

public PDFOutput(ArrayList<?> a)
{
arr = a;
}


public void GeneratePDF() {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
//addMetaData(document);
addTitlePage(document);
addContent(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private void addContent(Document document) throws DocumentException {
// TODO Auto-generated method stub
createTable(document);
}

private void addTitlePage(Document document) throws DocumentException {
// TODO Auto-generated method stub
Paragraph preface = new Paragraph();
// We add one empty line
addEmptyLine(preface, 1);
// Lets write a big header
preface.add(new Paragraph("Report", catFont));

addEmptyLine(preface, 2);
document.add(preface);

}

private void addEmptyLine(Paragraph paragraph, int number) {
// TODO Auto-generated method stub
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}

}


private void createTable(Document document)
throws DocumentException {
// Dynamically generates rows and columns
PdfPTable table = new PdfPTable(arr.size());

for(int i = 0; i < arr.size(); i++)
{
ArrayList<?> colArr = (ArrayList<?>)arr.get(i);


for(int j = 0; j < colArr.size(); j++)
{
PdfPCell c1 = new PdfPCell(new Phrase(colArr.get(j).toString()));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
c1.setFixedHeight(20);
table.addCell(c1);

}
}

document.add(table);

}

}

To test, create a class TestPDF.java
import java.io.FileWriter;
import java.util.ArrayList;


public class TestPDF {

/**
* @param args
*/
public static void main(String[] args) {

ArrayList records = new ArrayList();
ArrayList cell = new ArrayList();
cell.add("Name");
cell.add("Age");
cell.add("salary");
records.add(cell);

cell = new ArrayList();
cell.add("Ann");
cell.add(31);
cell.add(12000);
records.add(cell);

cell = new ArrayList();
cell.add("Alex");
cell.add(36);
cell.add(40000);
records.add(cell);


PdfOutput pdfOut = new PdfOutput(records);
pdfOut.GeneratePDF();

}

}

The program produced following output.

Comments

Popular posts from this blog

Transform values with a stream

Collections Framework

Inspect a collection