文档在 iText 7.0.2 上添加 Table 对象失败
Document failed adding Table object on iText 7.0.2
我最近将我的 iText7 项目从 7.0.1 升级到 7.0.2。之后,我尝试重建我的项目并进行了单元测试。单元测试给我一个错误,然后我尝试调试单元测试。结果是出现了一个我以前从未发现的奇怪异常...代码相同,但它在 v7.0.1 上顺利 运行,但在 v7.0.2 上捕获了以下异常:
Exception message:
"Nullable object must have a value."
Exception StackTrace:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource
resource) at iText.Layout.Renderer.TableRenderer.Layout(LayoutContext
layoutContext) at
iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer) at
iText.Layout.RootElement`1.Add(IBlockElement element) at
iText.Layout.Document.Add(IBlockElement element) at
KMK_Core.Core._Core_PrintKMK() in C:\Users\Jeffry
Angtoni\Source\Repos\kmk-core-ng\KMK NG\KMK Core\Core.cs:line 332
下面是我的 table 生成器代码:
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 对象不为空,但异常表明该对象可能为空...此修复的任何线索或解决方案...?
不幸的是,这确实是 7.0.2
中的一个错误,它与表格相对定位的不正确处理有关,并且存在于 Java 和 C# 中。它已经被修复,但修复将只在 7.0.3
版本中可用。但是,您始终可以从 Artifactory.
下载快照版本(在 C# 中也称为 7.0.2.1
)
我最近将我的 iText7 项目从 7.0.1 升级到 7.0.2。之后,我尝试重建我的项目并进行了单元测试。单元测试给我一个错误,然后我尝试调试单元测试。结果是出现了一个我以前从未发现的奇怪异常...代码相同,但它在 v7.0.1 上顺利 运行,但在 v7.0.2 上捕获了以下异常:
Exception message:
"Nullable object must have a value."
Exception StackTrace:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at iText.Layout.Renderer.TableRenderer.Layout(LayoutContext layoutContext) at iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer) at iText.Layout.RootElement`1.Add(IBlockElement element) at iText.Layout.Document.Add(IBlockElement element) at KMK_Core.Core._Core_PrintKMK() in C:\Users\Jeffry Angtoni\Source\Repos\kmk-core-ng\KMK NG\KMK Core\Core.cs:line 332
下面是我的 table 生成器代码:
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 对象不为空,但异常表明该对象可能为空...此修复的任何线索或解决方案...?
不幸的是,这确实是 7.0.2
中的一个错误,它与表格相对定位的不正确处理有关,并且存在于 Java 和 C# 中。它已经被修复,但修复将只在 7.0.3
版本中可用。但是,您始终可以从 Artifactory.
7.0.2.1
)