Thursday 10 March 2016

Read Table Details From Properties File and Create A Table Using iTextPdf


package test;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
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 PDFConversionDemoProperties {
public static void main(String[] args) {
try {

Properties prop = new Properties();
InputStream in = new FileInputStream(new File("E:\\Google Drive\\dev-workspace\\test\\src\\main\\resources", "pdftable.properties"));
prop.load(in);

OutputStream file = new FileOutputStream(new File("PDF.pdf"));

Document document = new Document();
PdfWriter.getInstance(document, file);

document.open();

//Create a table in PDF
PdfPTable pdfTable = new PdfPTable(4);

PdfPCell cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.1.1")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.1.2")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.1.3")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.1.4")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

pdfTable.addCell(emptyRow(pdfTable));


cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.3.1")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setRowspan(Integer.parseInt(prop.getProperty("generaltable.column.header.3.1.rowspan")));
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.3.2")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setRowspan(Integer.parseInt(prop.getProperty("generaltable.column.header.3.2.rowspan")));
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.3.3")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.3.4")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.4.3")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.4.4")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.5.3")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.5.4")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.6.3")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.6.4")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.7.1")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.7.2")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.7.3")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.7.4")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.8.1")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.8.2")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.8.3")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

cell1 = new PdfPCell(new Phrase(prop.getProperty("generaltable.column.header.8.4")));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfTable.addCell(cell1);

document.add(pdfTable);

document.close();
file.close();

} catch (Exception e) {

e.printStackTrace();
}
}

private static PdfPCell emptyRow(PdfPTable pdfPTable) {
PdfPCell cell = new PdfPCell(new Phrase(" "));
cell.setColspan(pdfPTable.getNumberOfColumns());
return cell;
}
}



#General Table
generaltable.column.header.1.1=Test11
generaltable.column.header.1.2=Test12-value
generaltable.column.header.1.3=Test13
generaltable.column.header.1.4=Test14-value

generaltable.column.header.2.1=EMPTY

generaltable.column.header.3.1=Test31
generaltable.column.header.3.1.rowspan=3
generaltable.column.header.3.2=Test32-value
generaltable.column.header.3.2.rowspan=3
generaltable.column.header.3.3=Test33
generaltable.column.header.3.4=Test34-value

generaltable.column.header.4.3=Test43
generaltable.column.header.4.4=Test44-value

generaltable.column.header.5.3=Test53
generaltable.column.header.5.4=Test54-value

generaltable.column.header.6.3=Test63
generaltable.column.header.6.4=Test64-value

generaltable.column.header.7.1=Test71
generaltable.column.header.7.2=Test72-value
generaltable.column.header.7.3=Test73
generaltable.column.header.7.4=Test74-value

generaltable.column.header.8.1=Test81
generaltable.column.header.8.2=Test82-value
generaltable.column.header.8.3=Test83
generaltable.column.header.8.4=Test84-value


Add caption