Here in this article, I am going to show you how we can create a pdf file with a table using java and iText. IText is a library to read and write pdf files. If you want to learn about iText visit https://itextpdf.com/en/get-started .
See below image we are going to create a pdf file like this

First, see the code below that is generating the above table.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
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 PdfGenerator {
public static void main( String[] args ) throws Exception
{
manipulatePdf("C:\\");
return;
}
public static void manipulatePdf(String dest) throws Exception {
Document doc = new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream("PdfTable.pdf"));
doc.open();
// Creates a table with four column. The first rows
// will have cell 1 to cell 4.
PdfPTable headerTable = new PdfPTable(1);
headerTable.setSpacingBefore(40f);
headerTable.setSpacingAfter(20f);
headerTable.setWidthPercentage(100);
PdfPTable assigneeTable = new PdfPTable(4);
assigneeTable.setSpacingAfter(20f);
assigneeTable.setWidthPercentage(100);
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
addRow(headerTable,1,"summary.....","");
//addRow(assigneeTable,1);
addAssigneeRow(assigneeTable);
addRow(table,2,"Field 1:","aaa");
addRow(table,2,"Field 2:","bbb");
addRow(table,2,"Field 3:","ccc");
addRow(table,2,"Field 4:","ddd");
addRow(table,2,"Field 5:","eee");
// Adds table to the doc
doc.add(headerTable);
doc.add(assigneeTable);
doc.add(table);
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
} finally {
doc.close();
}
}
public static void addRow(PdfPTable table,int columns,String title,String value) {
// Creates another row that only have to columns.
// The cell 5 and cell 6 width will span two columns
// in width.
BaseColor color = new BaseColor(240, 240, 240); // or red, green, blue, alpha
Font boldFont = new Font(Font.FontFamily.HELVETICA , 10, Font.BOLD);
Font normalFont = new Font(Font.FontFamily.HELVETICA , 11, Font.NORMAL);
PdfPCell cell1 = new PdfPCell(new Phrase(title,boldFont));
cell1.setColspan(1);
cell1.setPadding(5);
cell1.setBackgroundColor(color);//240
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell1);
if(columns>1) {
PdfPCell cell2 = new PdfPCell(new Phrase(value,normalFont));
cell1.setBackgroundColor(color);
cell2.setColspan(2);
cell2.setPadding(5);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell2);
}
table.completeRow();
}
public static void addAssigneeRow(PdfPTable table) {
// Creates another row that only have to columns.
// The cell 5 and cell 6 width will span two columns
// in width.
BaseColor color = new BaseColor(240, 240, 240); // or red, green, blue, alpha
Font boldFont = new Font(Font.FontFamily.HELVETICA , 10, Font.BOLD);
Font normalFont = new Font(Font.FontFamily.HELVETICA , 11, Font.NORMAL);
PdfPCell cell1 = new PdfPCell(new Phrase("Reporter:",boldFont));
cell1.setColspan(1);
cell1.setPadding(5);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setBackgroundColor(color);//240
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("User A",normalFont));
cell1.setBackgroundColor(color);
cell2.setPadding(5);
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase("Assignee:",boldFont));
cell3.setPadding(5);
cell3.setBackgroundColor(color);//240
table.addCell(cell3);
PdfPCell cell4 = new PdfPCell(new Phrase("User B",normalFont));
cell4.setPadding(5);
table.addCell(cell4);
table.completeRow();
}
}
If you are using maven add below dependency.
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
Many methods are self-explanatory. Here is the description of some of the methods.
setSpacingAfter(20f) //This will add space after a table.
setWidthPercentage(100) // Using this methods table will take full page width.
setVerticalAlignment(Element.ALIGN_MIDDLE) // Within cell if you want text to be vertically aligned.
setPadding(5) //Within cell if you want padding .
Don’t forget to call table.completeRow()
this will make sure that row is added into table.