元の質問へのコメントを何度か行ったり来たりした後でも、質問を正しく理解しているかどうかは完全にはわかりませんが、試してみましょう:
したがって、事前に列数がわからないが、列数とその幅を知るために最初の行のセルを取得する必要があると仮定します。その場合、次のように簡単に実行できます。
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);
}
}