Here's part of a report I wrote in which I address the exact problem you face. I looks like I used PdfPTable.TotalHeight.
Units are always Postscript points, except for font metrics.
#region Build the report
var queue = new Queue<ProductionReportSchema.Production_BuilderRow>(_Orders.Count);
foreach (var row in _Orders)
queue.Enqueue(row);
PdfPTable table = null;
var page = 1;
for (ProductionReportSchema.Production_BuilderRow row = null; queue.Count > 0; )
{
row = queue.Peek();
if (table == null)
{
document.NewPage();
content.BeginText();
content.SetFontAndSize(courierbold, 12);
content.ShowTextAligned(left, "Production Report - Batch " + Batch, 36, 564, 0);
content.ShowTextAligned(centre, DateTime.Now.ToString(), 396, 564, 0);
content.ShowTextAligned(right, "Page " + page++, 756, 564, 0);
content.EndText();
table = new PdfPTable(10);
table.SetTotalWidth(new float[] { 24, 24, 50, 50, 50, 72, 72, 72, 24, 288 });
table.DefaultCell.HorizontalAlignment = centre;
table.AddCell(new Phrase("Done", GridBoldStyle));
table.AddCell(new Phrase("0-9", GridBoldStyle));
table.AddCell(new Phrase("B", GridBoldStyle));
table.AddCell(new Phrase("Order", GridBoldStyle));
table.AddCell(new Phrase("Line", GridBoldStyle));
table.AddCell(new Phrase("Item", GridBoldStyle));
table.AddCell(new Phrase("Colour", GridBoldStyle));
table.AddCell(new Phrase("Icon", GridBoldStyle));
table.AddCell(new Phrase("Qty", GridBoldStyle));
table.DefaultCell.HorizontalAlignment = left;
table.DefaultCell.VerticalAlignment = top;
table.AddCell(new Phrase("Name", GridBoldStyle));
table.CompleteRow();
}
table.DefaultCell.PaddingBottom = 0;
table.DefaultCell.Border = Rectangle.NO_BORDER;
table.DefaultCell.BorderWidthTop = 0.25f;
table.DefaultCell.HorizontalAlignment = centre;
var box = new PdfPCell(table.DefaultCell);
box.AddElement(row.Quantity == 1 ? checkbox : doublecheckbox);
table.AddCell(box);
table.AddCell(new Phrase(row.Priority.ToString(), GridStyle));
table.AddCell(new Phrase("B" + row.id, GridStyle));
table.AddCell(new Phrase("O" + row.OrderNo, GridStyle));
table.AddCell(new Phrase("L" + row.ODid, GridStyle));
table.AddCell(new Phrase(row.ItemNo, GridStyle));
if (row.IsColourNull())
table.AddCell(string.Empty);
else
{
var ColourRow = Globals.Standards.Colours.FindByid(row.Colour);
table.AddCell(new Phrase(ColourRow == null ? string.Empty : ColourRow.Name_en, GridStyle));
}
if (row.IsIconNull())
table.AddCell(string.Empty);
else
{
var IconRow = Globals.Standards.Icons.FindByCode((short)row.Icon);
table.AddCell(new Phrase(IconRow == null ? string.Empty : IconRow.Name_en, GridStyle));
}
table.AddCell(new Phrase(row.Quantity.ToString(), GridStyle));
table.DefaultCell.HorizontalAlignment = centre;
var texts = new List<string>();
var xml = new XmlDocument();
var valid = true;
try { xml.LoadXml(row.MetaXml); }
catch (XmlException) { valid = false; }
var xpath1 = string.Format("//SubItem[@Code='{0}']", row.ItemNo);
var node1 = xml.SelectSingleNode(xpath1);
var nodes2 = xml.SelectNodes("//Text");
if (valid && node1 != null && node1.Attributes["GranularGroup"] == null)
{
foreach (XmlNode child in node1.ChildNodes)
if (child.Name == "Text")
texts.Add(child.InnerText);
}
else if (nodes2.Count != 0)
{
foreach (XmlNode node in nodes2)
{
texts.Add(node.InnerText);
if (nodes2.Count > 10 && node == nodes2[9])
break;
}
}
else
{
var TextLines = Regex.Matches(row.Name, @"(?<Output>.+?)(?<EOL>\|\||\r?\n|$)", RegexOptions.Multiline);
foreach (Match match in TextLines)
texts.Add(match.Groups["Output"].Value);
}
var TextCell = new PdfPCell(table.DefaultCell);
foreach (var text in texts)
if (Text.IsPrintable(text))
{
var barcode = new Barcode128();
barcode.Font = courier;
barcode.BarHeight = 12;
barcode.Code = text;
barcode.TextAlignment = left;
var bi = barcode.CreateImageWithBarcode(content, black, black);
bi.ScalePercent(100);
TextCell.AddElement(bi);
}
else
TextCell.AddElement(new Phrase(text, GridStyle));
table.AddCell(TextCell);
if (table.TotalHeight <= 500)
{
table.CompleteRow();
table.DefaultCell.PaddingBottom = 10;
table.DefaultCell.BorderWidthTop = 0f;
var CommentCell = new PdfPCell(table.DefaultCell);
CommentCell.AddElement(new Phrase(row.Comments, GridStyle));
CommentCell.Colspan = 10;
table.AddCell(CommentCell);
table.CompleteRow();
queue.Dequeue();
}
else
{
table.DeleteLastRow();
table.WriteSelectedRows(0, -1, 36, 540, content);
table = null;
}
}
if(table != null)
table.WriteSelectedRows(0, -1, 36, 540, content);
document.Close();
var PdfViewer = new PdfViewer();
PdfViewer.Filename = filename;
PdfViewer.Show();
#endregion