Setting the table height does not make sense as soon as you start thinking about it. Or it makes sense, but leaves many questions unanswered or unanswered. For example, if you set a two-row table to a height of 500, does this mean that each cell gets 250 for height? What if a large image falls in the first row, if the table automatically responds by splitting 400/100? Then what about the large content on both lines, should it squish them? Each of these scenarios gives different results, which give an idea that the table will indeed be unreliable. If you look at the HTML specification , you will see that they do not even allow you to set a fixed height for tables.
However, there is a simple solution and just setting a fixed height for the cells themselves. Until you use new PdfPCell() , you can simply set DefaultCell.FixedHeight to whatever you want.
var t = new PdfPTable(2); t.DefaultCell.FixedHeight = 100f; t.AddCell("Hello"); t.AddCell("World"); t.AddCell("Hello"); t.AddCell("World"); doc.Add(t);
If you manually create cells, you need to set FixedHeight for each:
var t = new PdfPTable(2); for(var i=0;i<4;i++){ var c = new PdfPCell(new Phrase("Hello")); c.FixedHeight = 75f; t.AddCell(c); } doc.Add(t);
However, if you want a normal table and need to set a fixed height that interrupts anything that doesn't fit, you can also use ColumnText . I would not recommend this, but you may have a case. In the code below, only six lines will be displayed.
var ct = new ColumnText(writer.DirectContent); ct.SetSimpleColumn(100, 100, 200, 200); var t = new PdfPTable(2); for(var i=0;i<100;i++){ t.AddCell(i.ToString()); } ct.AddElement(t); ct.Go();
source share