Alberto Sartori Alberto Sartori - 1 month ago 12
C# Question

Rowspan with iTextSharp doesn't work properly

I'm trying to create a simple table with iTextSharp, so with C#. The goal is a table like this one:



The problem is that if I apply the rowspan as 2 on cell A, iTextSharp does not render the rowspanned cell, this means that the cell have the same height of cell B. Here's the code:

PdfPTable corporateTable = new PdfPTable(2);
corporateTable.HeaderRows = 1;
corporateTable.TotalWidth = pdfWidth - 50;

PdfPCell vCell = new PdfPCell();
vCell.Border = Rectangle.BOX;
vCell.Rowspan = 2;
vCell.Phrase = new Phrase("A", new Font(fontLh, 7f, 1, BaseColor.BLACK));
corporateTable.CompleteRow();
corporateTable.AddCell(vCell);


PdfPCell vCellx = new PdfPCell();
vCellx.Phrase = new Phrase("B", new Font(fontLh, 7f, 1, BaseColor.BLACK));
vCellx.Colspan = 3;
corporateTable.AddCell(vCellx);

PdfPCell vCell1 = new PdfPCell();
vCell1.Phrase = new Phrase("C", new Font(fontValue, 7f, 0, BaseColor.BLACK));
corporateTable.AddCell(vCell1);

corporateTable.WriteSelectedRows(0, -1, 100f, 100f, writer.DirectContent);
document.Close();


What's wrong? I'm using the latest version of the dll.

Answer

Well the basic answer is: it works! if you add two more cells, you will see that one cell (the one underneath A) is not filled.

But this is not what you expect (nor did I btw). To achive what you want use nested tables, that means:

  • create a table with two cols
  • insert cell A into table
  • create cell 2
  • create one more table with 1 col
    • insert cell B into table 2
    • insert cell c into table 2
  • insert table 2 into cell 2

search for itext rowspan, you will find multiple fully typed out examples.

hth

Mario