PdfTable: last cell is not visible

不想你离开。 提交于 2019-12-18 09:18:51

问题


I use PdfPTable in the following way

var myTable = new PdfPTable( 3 );

foreach(var nextString in myStrings)
{

var nextCell = new PdfPCell( new Phrase( nextString, smallFont ) );
nextCell.Border = Rectangle.NO_BORDER;
nextCell.AddCell(nextCell);
}

pdfDocument.Add(myTable);

All is perfect then total cell count multiply the number of columns (3). But when I wanted to create a table with 3 columns but 4 cells - last row is not visible.

How to solve such issue?

itextsharp 5.3.3.0


回答1:


To solve this issue and others I recommend the use of the PdfPTable method CompleteRow().

This will ensure that any incomplete rows have extra cells added to make them complete so that they show up in the generated PDF.

Generally, an incomplete row will be a logic error however by using CompleteRow() you can easily identify where you have made these errors, and correct them.

In terms of your example:

var myTable = new PdfPTable( 3 );

foreach(var nextString in myStrings)
{
    var nextCell = new PdfPCell( new Phrase( nextString, smallFont ) );
    nextCell.Border = Rectangle.NO_BORDER;
    nextCell.AddCell(nextCell);
}

myTable.CompleteRow();

pdfDocument.Add(myTable);



回答2:


You can either fill the last row until it's fill or you can also set it's ColumnSpan to occupy the empty space.




回答3:


The last row is not visible because the last row is not complete. In your case with 3 columns and 4 cells, you need to add 2 more empty cells to make the last row complete.




回答4:


The CompleteRow() method will do the thing. This adds an empty cell at the end of your PDF, but you will notice it displays an empty cell with border. So, in order to avoid this behaviour, you will have to set DefaultCell.Border = Rectangle.NO_BORDER before calling the CompleteRow() method.

var myTable = new PdfPTable( 3 );

foreach(var nextString in myStrings)
{

    var nextCell = new PdfPCell( new Phrase( nextString, smallFont ) );
    nextCell.Border = Rectangle.NO_BORDER;
    nextCell.AddCell(nextCell);
}
myTable.DefaultCell.Border = Rectangle.NO_BORDER;
myTable.CompleteRow();

pdfDocument.Add(myTable);


来源:https://stackoverflow.com/questions/12709428/pdftable-last-cell-is-not-visible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!