How to create a nested column?

I need help creating a nested table in iText.

31st May 2016
admin-marketing

How can I create a table that looks like the table in the following screen shot:

Screen shot

This is my code so far:

 

var subTable2 = new PdfPTable(new float[] { 100, 100, 100, 100, 100, 100,100,100 });
subTable2.TotalWidth = 510f;
subTable2.LockedWidth = true;
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Examination", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Board", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Month and Year of Passing", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new PdfPCell(new Phrase("Mark", time51)));
PdfPTable nested = new PdfPTable(1);
nested.AddCell("");
nested.AddCell("Obtained");
nested.AddCell("Out of");
PdfPCell nesthousing = new PdfPCell(nested);
nesthousing.Padding = 0f;
subTable2.AddCell(nesthousing);
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Percentage", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Calss/Grade", time51));
doc.Add(subTable2);
Posted on StackOverflow on Jul 7, 2015 by Semil Sebastian

Please take a look at SimpleTable12. It creates a table that looks like this: simple_table12.pdf

Table example

Table example

You could still fine-tune the height of the cells by using a padding, but those are details. What matters is: how to organize the cells?

I've written this small convenience method:

public Cell createCell(String content, int colspan, int rowspan, int border) {
    Cell cell = new Cell(rowspan, colspan).add(new Paragraph(content));
    cell.setBorder(null);
    if (8 == (border & 8)) {
        cell.setBorderRight(new SolidBorder(1));
        cell.setBorderBottom(new SolidBorder(1));
    }
    if (4 == (border & 4)) {
        cell.setBorderLeft(new SolidBorder(1));
    }
    if (2 == (border & 2)) {
        cell.setBorderBottom(new SolidBorder(1));
    }
    if (1 == (border & 1)) {
        cell.setBorderTop(new SolidBorder(1));
    }
    return cell;
}

You pass some content, a colspan, a rowspan and border info and it creates you the Cell you need. This allows us to create code that is easier to read:

public void createPdf(String dest) throws Exception {
    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
    Document doc = new Document(pdfDoc, PageSize.A4.rotate());

    font = PdfFontFactory.createFont(FontConstants.HELVETICA);
    Table table = new Table(8);

    table.setTextAlignment(TextAlignment.CENTER);
    table.setFont(font).setFontSize(10);

    table.addCell(createCell("Examination", 1, 2, 15));
    table.addCell(createCell("Board", 1, 2, 15));
    table.addCell(createCell("Month and Year of Passing", 1, 2, 15));
    table.addCell(createCell("", 1, 1, 1));
    table.addCell(createCell("Marks", 2, 1, 1));
    table.addCell(createCell("Percentage", 1, 2, 15));
    table.addCell(createCell("Class / Grade", 1, 2, 15));
    table.addCell(createCell("", 1, 1, 15));
    table.addCell(createCell("Obtained", 1, 1, 15));
    table.addCell(createCell("Out of", 1, 1, 15));
    table.addCell(createCell("12th / I.B. Diploma", 1, 1, 15));
    table.addCell(createCell("", 1, 1, 15));
    table.addCell(createCell("", 1, 1, 15));
    table.addCell(createCell("Aggregate (all subjects)", 1, 1, 15));
    table.addCell(createCell("", 1, 1, 15));
    table.addCell(createCell("", 1, 1, 15));
    table.addCell(createCell("", 1, 1, 15));
    table.addCell(createCell("", 1, 1, 15));

    doc.add(table);
    doc.close();
}

As you can see, it's a matter of calculating the correct colspan and rowspan values, and applying the correct borders.

Click this link if you want to see how to answer this question in iText 5.


Share this article

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now