0

iTextSharp バージョン 5.4.5.0 を使用しています。

複数の PdfPCell で PdfPTable を印刷しようとしています。PdfPCell の数は動的になります。では、動的に生成された PdfPCell に幅を割り当てるにはどうすればよいですか?

セルの静的および固定数に幅を割り当てる方法を知っています。しかし、動的セルの場合、動的に生成された各セルに幅を割り当てるにはどうすればよいですか? PdfPCell の数は固定ではありません。

私を助けてください ?

ありがとう。

4

1 に答える 1

2

元の質問へのコメントを何度か行ったり来たりした後でも、質問を正しく理解しているかどうかは完全にはわかりませんが、試してみましょう:

したがって、事前に列数がわからないが、列数とその幅を知るために最初の行のセルを取得する必要があると仮定します。その場合、次のように簡単に実行できます。

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);
    }
}
于 2016-02-22T15:46:43.833 に答える