Reputation: 5
I am writing a Java code to generate a PDF template. In the pdf's header section, I have created a pdfTable which has 7 cells including an image cell (Logo), a text field (Id Number) and remaining 5 cells to populate the actual Id Number.
In the output, I should get a bigger image cell (representing the Logo) and Id number cells must be smaller in size than the image cell. Example, as in the below image (Expected Result).
However when the template is generated, I am unable populate as expected as shown in the above image (Expected Result).
When the PDF is generated, all the cells are taking the size of the image cell.
I have tried with different approaches like setting column widhts, setFixedHeight(), setRowSpan(), setColumnSpan() methods etc. But nothing worked. Below image shows my output (Current Output).
Below is code which I have written.
public class NbaBafTemplateGenerator {
private void createNbaBafTemplate(File outPutfileName, NbaBafTemplateData formData,String logoName) {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPutfileName));
document.open();
PdfPTable table = null;
// Passing the data as a single String
//IdNumber varible is of type String and has 5 characters of the number.
table = NbaBafTemplatePage.createHeaderTable(logoName + ",Id Number: , " +
formData.getIdNumber(), 7, "", "","1.5f, 1f, 0.2f, 0.2f, 0.2f, 0.2f, 0.2f");
document.add(table);
}// END OF CLASS NbaBafTemplateGenerator.
//Class NbaBafTemplatePage Begins.
public class NbaBafTemplatePage extends PdfPageEventHelper {
public static PdfPTable createHeaderTable(String text, int columnCount, String colour, String align, String colSize)
throws DocumentException, IOException {
PdfPTable table = null;
table = new PdfPTable(columnCount); // 7 columns.
table.setWidthPercentage(100); // Width 100%
table.setSpacingBefore(0f); /
table.setSpacingAfter(10f);
//Assigning column widths based on input width params.
float[] tablecolumnWidths = {
Float.parseFloat(colSize.split(",")[0]),
Float.parseFloat(colSize.split(",")[1]),
Float.parseFloat(colSize.split(",")[2]),
Float.parseFloat(colSize.split(",")[3]),
Float.parseFloat(colSize.split(",")[4]),
Float.parseFloat(colSize.split(",")[5]),
Float.parseFloat(colSize.split(",")[6])};
PdfPCell imgCell = new PdfPCell(createImageCell(text.split(",")[0]));
//imgCell.setColspan(3);
//imgCell.setRowspan(3);
imgCell.setBorder(PdfPCell.NO_BORDER);
imgCell.setHorizontalAlignment(Element.ALIGN_LEFT);
imgCell.setVerticalAlignment(Element.ALIGN_LEFT);
table.addCell(imgCell);
PdfPCell idCell = new PdfPCell(new Paragraph(text.split(",")[1]));
idCell.setBorderColor(BaseColor.BLACK);
idCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
idCell.setPaddingLeft(10);
idCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
idCell.setVerticalAlignment(Element.ALIGN_RIGHT);
table.addCell(idCell);
PdfPCell cellC0 = new PdfPCell(new Paragraph(text.split(",")[2]));
cellC0.setBorderColor(BaseColor.BLACK);
cellC0.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC0.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC0);
PdfPCell cellC1 = new PdfPCell(new Paragraph(text.split(",")[3]));
cellC1.setBorderColor(BaseColor.BLACK);
cellC1.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC1);
PdfPCell cellC2 = new PdfPCell(new Paragraph(text.split(",")[4]));
cellC2.setBorderColor(BaseColor.BLACK);
cellC2.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC2.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC2);
PdfPCell cellC3 = new PdfPCell(new Paragraph(text.split(",")[5]));
cellC3.setBorderColor(BaseColor.BLACK);
cellC3.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC3.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC3);
PdfPCell cellC4 = new PdfPCell(new Paragraph(text.split(",")[6]));
cellC4.setBorderColor(BaseColor.BLACK);
cellC4.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC4.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC4);
return table;
}//END OF METHOD createHeaderTable.
public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
Image img = Image.getInstance(path);
PdfPCell cell = new PdfPCell(img, true);
return cell;
}
}
I am using Java and iText 5.x version.
Can anyone please let me know how to generate the pdf table with different cell sizes.
Upvotes: 0
Views: 1889
Reputation:
You can create a table with 2 rows (13 cells). Set the column-span of image cell equal to 2. Keep the remaining cells of the first row as blank, set their borders as 0 (invisible), and adjust their heights (cells of the first row) as per your required alignment. Then add the remaining 6 cells to the table as a second row. Adjust column widths as per your requirement. Hope this helps.
PdfPTable table = new PdfPTable(7);
table.setWidthPercentage(100);
PdfPCell imageCell = new PdfPCell(image);
imageCell.setBorder(0);
imageCell.setRowspan(2);
table.addCell(imageCell);
for(int i=0; i<6;i++) {
PdfPCell blankCell = new PdfPCell();
blankCell.setBorder(0);
blankCell.setFixedHeight(20f);
table.addCell(blankCell);
}
PdfPCell cell22 = new PdfPCell(new Phrase("ID Number"));
table.addCell(cell22);
PdfPCell cell23 = new PdfPCell(new Phrase("9"));
table.addCell(cell23);
PdfPCell cell24 = new PdfPCell(new Phrase("6"));
table.addCell(cell24);
PdfPCell cell25 = new PdfPCell(new Phrase("0"));
table.addCell(cell25);
PdfPCell cell26 = new PdfPCell(new Phrase("5"));
table.addCell(cell26);
PdfPCell cell27 = new PdfPCell(new Phrase("1"));
table.addCell(cell27);
document.add(table);
Upvotes: 2