ItextSharp, number of Cells not dividable by the l

2019-02-27 03:04发布

问题:

I am currently working on a C# project which requires me to make a Dynamic Table. (The user of the program can decide how many Cells a row contains.)

That bit I have figured out, but here comes the problem. Say for example the user wants the data displayed in rows of 3 but there are 5 things that need to be displayed it will only show the first 3 and the last 2 disappear.

How can I make it so that if the number of cells can't be divided by the length of the row (like 4 cells with rows of 3 or 5 cells with rows of 2) that an empty cell is added so the last row is also displayed (or any other fix that displays the last row where the cells are not enough to fill that row)?

Example code:

        PdfPTable card = new PdfPTable(3);

        card.AddCell("Row 1 cell 1");
        card.AddCell("Row 1 cell 2");
        card.AddCell("Row 1 cell 3");
        card.AddCell("Row 2 cell 1");
        card.AddCell("Row 2 cell 2");
        card.AddCell("Row 2 cell 3");
        card.AddCell("Row 3 cell 1 Not showing");
        card.AddCell("Row 3 cell 2 Not showing");

        doc.Add(card);

回答1:

There's a method on PdfPTable called CompleteRow that does exactly what you are talking about. Just call it when you are done with the table and it will "fill in the blanks" with the table's "default cell".



标签: c# pdf itext row