1
votes

I'm trying to create table without borders with 4 columns and 2 rows on top of my PDF document. The problem is that the 2nd row won't be written. This is my code:

float[] columnWidths = { 2, 1, 1, 1};
PdfPTable table = new PdfPTable(columnWidths);
table.WidthPercentage = 100;
if (...) //true
{
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("AAA:_______________",infoFont));    
                p.BorderWidth = 0;  
                table.AddCell(p);  // fixed pos. 1st col,1st row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("BBB:_____", infoFont));   
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 2nd col,1st row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("CCC:_____", infoFont));    
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 3rd col,1st row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("DDD:_____", infoFont));      
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 4th col,1st row
       }
}
if (...) //true
{
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("EEE: " + eee));
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 1st col,2nd row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("FFF: " + fff));
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 2nd col,2nd row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("GGG: " + ggg));
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 3rd col,2nd row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("HHH:___________________"));
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 4th col,2nd row
       }
}

document.Add(table);

How can I deal with this? And the 2nd question: can I have fixed position for every if condition (check the comments in the code) so when one if-condition in first row is not true then that cell should be empty?

1

1 Answers

4
votes

I assume that you've simplified your code to the extent that the snippet you share is no longer consistent with your own code. You are creating a table with 4 columns. If you add 4 cells, one row will be rendered. If you add 8 cells, two rows will be rendered. However: if you only add 7 cells, then a single row will be added. The 3 cells in the incomplete row will be omitted because iText only renders complete rows.

See also How to generate pdf if our column less than the declared table column and ItextSharp, number of Cells not dividable by the length of the row and Odd Numbered Cell Not Added To Pdf and PdfTable: last cell is not visible and ...

This explains why the second row isn't shown. Add the following line to see if this fixes the problem:

table.CompleteRow();

As for your other question: you can always add an empty cell like this:

PdfPCell cell = new PdfPCell();
if (someCondition) {
    cell.addElement(new Paragraph("AAA"));
} 
table.addCell(cell);

Finally, there's another error in your code. This doesn't make any sense:

p.BorderWidth = 0;

A border width of 0 doesn't mean that no border will be shown. As explained many times before ISO-32000-1 defines a line with 0 width as a line of which the width is equal to the minimal width that can be displayed by the device. If you don't want any border use:

p.Border = PdfPCell.NO_BORDER;

Finally, I need to ask you a favor: we've redesigned the iText web site and we released it on Thanksgiving. We now notice that we don't get as many visits as we used to before the change. Given the fact that all the information you needed can be found on the online documentation and given the fact that you still needed to ask the question, we'd like to know what is wrong with the web site. Is there something we can do to improve the content? What could be the reason that drives people away from our web site? Why are you asking so many questions that are already answered in the official documentation? Do we have too much content now?