.net 中 iText 的嵌套 Table 问题

Nested Table issue with iText in .net

我将 iText 7.0.4.0 与我的 .net 应用程序一起使用来生成 pdf。但是当文本很长时,内部 tables 会溢出。

外部 table 有 10 列带有绿色边框,看起来它已经按照下图渲染得很好。每个外部 table 单元格包含一个 table 和一个内部单元格 it.But 当段落文本较大时,内部 Table 单元格溢出。

我在大型 Forms 构建产品中使用 iText。因此,我用简单的场景重新创建了这个问题,下面给出了代码。请注意,列数在实际使用中是不固定的。

任何人都可以告诉我实现此目标的正确途径吗?

这是 C# 代码

private Table OuterTable()
{
    var columns = GetTableColumnWidth(10);
    var outerTable = new Table(columns, true);
    outerTable.SetWidthPercent(100);

    for (int index = 0;   index < columns.Length; index++)
    {
        Cell outerTableCell = new Cell();

        Table innerTable = new Table(new float[] { 100 });
        innerTable.SetWidthPercent(100);
        Cell innerTableCell = new Cell();
        Paragraph paragraph = new Paragraph("ABCDEFGHIJKL").AddStyle(_fieldValueStyle);

        innerTableCell.Add(paragraph);
        innerTable.AddCell(innerTableCell);

        outerTableCell.Add(innerTable);
        outerTable.AddCell(outerTableCell);

        innerTableCell.SetBorder(new SolidBorder(Color.RED, 2));
        innerTableCell.SetBorderRight(new SolidBorder(Color.BLUE, 2));
        outerTableCell.SetBorder(new SolidBorder(Color.GREEN, 2));
    }

    return outerTable;
}

感谢mkl 花费您宝贵的时间。我用你 'no inner tables' 的想法解决了我的问题。这不是解决问题中提到的嵌套表问题的方法,而是另一种实现结果的方法。

我在段落中使用了“\n”来实现我想要的。这是输出和代码。

private Table OuterTable()
    {
        var columns = GetTableColumnWidth(10);
        var outerTable = new Table(columns, true);
        outerTable.SetWidthPercent(100);

        for (int index = 0; index < columns.Length; index++)
        {
            Cell outerTableCell = new Cell();

            outerTableCell.Add(GetContent());
            outerTable.AddCell(outerTableCell);
        }

        return outerTable;
    }

    private Paragraph GetContent()
    {
        int maxIndex = 3;
        Paragraph paragraph = new Paragraph();
        for (int index = 0; index < maxIndex; index++)
        {
            paragraph.Add(index + " - ABCDEFGHIJKL \n").AddStyle(_fieldValueStyle);
        }
        return paragraph;
    }