Even after some back and forth in comments to the original question, I am not entirely sure I understand the question correctly, but let's try:
So let us assume you do not know the number of columns beforehand but need to fetch the cells of the first row to get to know the number of columns and their widths. In that case you can simply do something like this:
public void CreatePdfWithDynamicTable()
{
using (FileStream output = new FileStream(@"test-results\content\dynamicTable.pdf", FileMode.Create, FileAccess.Write))
using (Document document = new Document(PageSize.A4))
{
PdfWriter writer = PdfWriter.GetInstance(document, output);
document.Open();
PdfPTable table = null;
List<PdfPCell> cells = new List<PdfPCell>();
List<float> widths = new List<float>();
for (int row = 1; row < 10; row++)
{
// retrieve the cells of the next row and put them into the list "cells"
...
// if this is the first row, determine the widths of these cells and put them into the list "widths"
...
// Now create the table (if it is not yet created)
if (table == null)
{
table = new PdfPTable(widths.Count);
table.SetWidths(widths.ToArray());
}
// Fill the table row
foreach (PdfPCell cell in cells)
table.AddCell(cell);
cells.Clear();
}
document.Add(table);
}
}
Listinstead of aPdfPTable. As soon as the cells for the first row are finished, you know the number of columns of your table, can create aPdfPTablewith exactly that number of columns, and add the cells from the list to the table. The next rows can then be added as usual. - mklPdfPTableafter creating the cells of the first row, you then can set the cell widths. - mkl