【问题标题】:Nested Table issue with iText in .net.net 中 iText 的嵌套表问题
【发布时间】:2018-08-07 09:34:50
【问题描述】:

我使用 iText 7.0.4.0 和我的 .net 应用程序来生成 pdf。但是当文本很长时,内表会溢出。

外表有 10 列,带有绿色边框,如下图所示。每个外表单元格包含一个表,其中一个单元格。但是当段落文本很大时,内表单元格已经溢出。

我在大型表单构建产品中使用 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;
}

【问题讨论】:

  • 请使用当前的 iText 7(例如 7.1.2)重试。表创建代码经历了许多改进。
  • 根据您的建议,我尝试使用 7.1.2.0。结果还是一样。 :(
  • 在这种情况下,请分享 GetTableColumnWidth(10) 返回的内容以允许重现问题。
  • GetTableColumnWidth(10) 返回具有列宽的浮点数组。请使用 var columns = new float[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };对于上述情况。我还注意到,仅当外部表具有大量列(例如:10)时才会出现上述问题。它适用于 4 列。
  • 好的。话虽如此,您希望结果如何?标准尺寸的十次"ABCDEFGHIJKL" 根本不适合。您是否希望将段落分成多行?或者你想让它们用更小的字体?

标签: itext itext7


【解决方案1】:

感谢 mkl 花费您宝贵的时间。我用你的“没有内表”的想法解决了我的问题。这不是如何解决问题中提到的嵌套表问题,而是另一种实现结果的方法。

我在段落中使用了“\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;
    }

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 2010-11-04
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多