Search code examples
c#pdfitextitext7

Table header cell has a breakline and the other headers don't change their height


Im adding a large text to the header cell and the other cell of the table doesnt change their height image.

Here is the code for my table, what should I add to it?

Table tblChcklist = new Table(UnitValue.CreatePercentArray(new float[] { 4, 35, 20, 20, 20 })).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER).UseAllAvailableWidth().SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA, PdfEncodings.WINANSI, false));
tblChcklist.SetKeepTogether(true).SetFontSize(10).SetFixedLayout();
tblChcklist.AddHeaderCell(handler.getCell(2, 1, "No.", TextAlignment.CENTER).SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
tblChcklist.AddHeaderCell(handler.getCell(2, 1, $"Seccion {dtPreguntas.Rows[0]["nombreSeccion"] }", TextAlignment.LEFT).SetBold());
tblChcklist.AddHeaderCell(handler.getCell(0, 0, "Si", TextAlignment.CENTER));
tblChcklist.AddHeaderCell(handler.getCell(0, 0, "No", TextAlignment.CENTER));
tblChcklist.AddHeaderCell(handler.getCell(0, 0, "No Aplica", TextAlignment.CENTER));

And here is the handle.getCell method

public Cell getCell(int rowspan,int colspan,String text, TextAlignment alignment)
{
    Cell cell = new Cell(rowspan,colspan).Add(new Paragraph(text));
    cell.AddStyle(new Style().SetBorderBottom(new SolidBorder(ColorConstants.BLACK,0)));
    cell.AddStyle(new Style().SetBorderTop(new SolidBorder(ColorConstants.BLACK, 0)));
    cell.SetPadding(0);
    cell.SetTextAlignment(alignment);
    cell.SetKeepTogether(true);
    return cell;
}

Solution

  • If I understand you correctly, you do not want to see the following space (highlighted with yellow): enter image description here

    However, it's not some strange space, but place for cells of the second row, which haven't been added yet. This happens because you've added the first ("No") and the second ("Seccion...") cells with rowspan 2, therefore they occupy space on both rows. However, you've filled only the first row by adding just 5 cells, so the second tow is empty, hence you see some space there.

    So there are two solutions here: either add more cells (if its you intention) or do not set the rowspan's value of the first two cells as 2. I believe the latter should be the case, since, if I get the idea of the snippet you shared, this big rowspan is set mistakenly.