【问题标题】:Document failed adding Table object on iText 7.0.2文档在 iText 7.0.2 上添加表格对象失败
【发布时间】:2017-02-21 07:23:06
【问题描述】:

我最近将 iText7 项目从 7.0.1 升级到了 7.0.2。之后,我尝试重建我的项目并进行了单元测试。单元测试给了我一个错误,然后我尝试调试单元测试。结果是出现了一个我以前从未发现过的奇怪异常...代码相同,但在 v7.0.1 上运行流畅,但在 v7.0.2 上捕获了以下异常:

异常信息:

“可空对象必须有一个值。”

异常堆栈跟踪:

在 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource 资源)在 iText.Layout.Renderer.TableRenderer.Layout(LayoutContext 布局上下文)在 iText.Layout.Renderer.RootRenderer.AddChild(IRenderer 渲染器)在 iText.Layout.RootElement`1.Add(IBlockElement 元素)在 iText.Layout.Document.Add(IBlockElement 元素)在 C:\Users\Jeffry 中的 KMK_Core.Core._Core_PrintKMK() Angtoni\Source\Repos\kmk-core-ng\KMK NG\KMK Core\Core.cs:line 332

下面是我的表格生成器代码:

Table tblStudentIdentity = new Table(new float[] { 3.5f, .2f, 5f });
tblStudentIdentity.SetWidthPercent(80f);

_Core_SetIdentityTableValue(ref tblStudentIdentity, "NAMA", "Name", "LEONARDO DICAPRIO");
_Core_SetIdentityTableValue(ref tblStudentIdentity, "NIM", "Student Number", "1301566255");
_Core_SetIdentityTableValue(ref tblStudentIdentity, "FAKULTAS / SEKOLAH", "Faculty/School", "", "School of Computer Science");

tblStudentIdentity.SetRelativePosition(115f, 20f, 0f, 0f);
_doc.Add(tblStudentIdentity);

下面是我的 _Core_SetIdentityTableValue 函数:

private int _Core_SetIdentityTableValue(ref Table tableObject, string LeftLabelID = "", string LeftLabelEN = "", string RightValueID = "", string RightValueEN = "")
{
    int Result = 0;
    Text txtOpenInfo = new Text("(").AddStyle(StyleHelper.lblIDStyle);
    Text txtCloseInfo = new Text(")").AddStyle(StyleHelper.lblIDStyle);
    Text txtColonInfo = new Text(":").AddStyle(StyleHelper.lblIDStyle);

    Paragraph lblLeftPrgrh = new Paragraph();
    if (LeftLabelEN != "" && LeftLabelID != "")
    {
        Text lblID = new Text(LeftLabelID + " ").AddStyle(StyleHelper.lblIDStyle);
        Text lblEN = new Text(LeftLabelEN).AddStyle(StyleHelper.lblENStyle);
        lblLeftPrgrh.Add(lblID).Add(txtOpenInfo).Add(lblEN).Add(txtCloseInfo);
    }else if (LeftLabelEN == "" && LeftLabelID != "")
    {
        Text lblID = new Text(LeftLabelID).AddStyle(StyleHelper.lblIDStyle);
        lblLeftPrgrh.Add(lblID);
    }else if (LeftLabelEN != "" && LeftLabelID == "")
    {
        Text lblEN = new Text(LeftLabelEN).AddStyle(StyleHelper.lblENStyle);
        lblLeftPrgrh.Add(lblEN);
    }else
    {
        Text lblErrorLeft = new Text("Error-NoValue");
        lblLeftPrgrh.Add(lblErrorLeft);
        Result = 1;
    }

    Paragraph lblRightPrgrh = new Paragraph();
    if (RightValueEN != "" && RightValueID != "")
    {
        Text lblValID = new Text(RightValueID + " ").AddStyle(StyleHelper.lblIDStyle);
        Text lblValEN = new Text(RightValueEN).AddStyle(StyleHelper.lblENStyle);
        lblRightPrgrh.Add(lblValID).Add(txtOpenInfo).Add(lblValEN).Add(txtCloseInfo);
    }else if (RightValueEN == "" && RightValueID != "")
    {
        Text lblValID = new Text(RightValueID).AddStyle(StyleHelper.lblIDStyle);
        lblRightPrgrh.Add(lblValID);
    }else if (RightValueEN != "" && RightValueID == "")
    {
        Text lblValEN = new Text(RightValueEN).AddStyle(StyleHelper.lblENStyle);
        lblRightPrgrh.Add(lblValEN);
    }else
    {
        Text lblValError = new Text("Error-NoValue");
        lblRightPrgrh.Add(lblValError);
        Result = 1;
    }

    Paragraph lblCenterPrgrh = new Paragraph(txtColonInfo);

    Cell cellLeft = new Cell();
    cellLeft.Add(lblLeftPrgrh)
        .SetBorder(Border.NO_BORDER)
        .SetVerticalAlignment(VerticalAlignment.TOP);
    Cell cellMiddle = new Cell();
    cellMiddle.Add(lblCenterPrgrh)
        .SetBorder(Border.NO_BORDER)
        .SetVerticalAlignment(VerticalAlignment.TOP);
    Cell cellRight = new Cell();
    cellRight.Add(lblRightPrgrh)
        .SetBorder(Border.NO_BORDER)
        .SetVerticalAlignment(VerticalAlignment.TOP);
    tableObject.AddCell(cellLeft).AddCell(cellMiddle).AddCell(cellRight);
    return Result;
}

这是 v7.0.2 中的错误,因为 v7.0.1 已成功运行上述代码...?我还检查了 Table 对象不为空,但异常告诉该对象可能为空...此修复的任何线索或解决方案...?

【问题讨论】:

    标签: c# itext itext7


    【解决方案1】:

    不幸的是,这确实是7.0.2 中的一个错误,它与表的相对定位处理不正确有关,并且存在于 Java 和 C# 中。它已被修复,但修复仅在7.0.3 版本中可用。但是,您始终可以从 Artifactory 下载快照版本(在 C# 中也称为 7.0.2.1)。

    【讨论】:

    • 好的...我会等待下一个版本...现在我将回滚到v7.0.1,因为代码运行成功......
    猜你喜欢
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多