I want to generate a PDF with 3 columns by dynamic data. I have tried with my this code,
public class Clazz {
public static final String RESULT = "result.pdf";
private String[] data = {"1","2","3","4","5","6","7","8"};
private void go() throws Exception {
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
doc.open();
PdfPTable mainTable = new PdfPTable(3);
PdfPCell cell;
for (int i = 0; i < data.length; i+=2) {
cell = new PdfPCell(new Phrase(data[i]));
PdfPTable table = new PdfPTable(1);
table.addCell(cell);
if (i+1 <= data.length -1) {
cell = new PdfPCell(new Phrase(data[i + 1]));
table.addCell(cell);
} else {
cell = new PdfPCell(new Phrase(""));
table.addCell(cell);
}
mainTable.addCell(table);
}
doc.add(mainTable);
doc.close();
}
}
I want to print like this in my PDF:
In first page it will print 1 2 3 and then in next page it will print 4 5 6 and then in next page it will print 7 8 and blank cell.