我最近將我的iText7項目從7.0.1升級到7.0.2。之後,我試圖重建我的項目並進行單元測試。單元測試給了我一個錯誤,然後我試着調試單元測試。其結果是有,我從來沒有發現一個奇怪的例外...該代碼是一樣的,但它在V7.0.1流暢運行,但低於異常風行V7.0.2:文檔無法在iText 7.0.2上添加表格對象
異常消息:
「可爲空的對象必須有值。」
異常堆棧跟蹤:
在System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource 資源)在iText.Layout.Renderer.TableRenderer.Layout(LayoutContext layoutContext)在 iText.Layout.Renderer.RootRenderer.AddChild(IRenderer渲染器)在 iText.Layout.RootElement`1.Add(IBlockElement元件)在 iText.Layout.Document.Add(IBlockElement元件)在 KMK_Core.Core._Core_PrintKMK()在C:\用戶\傑弗裏 Angtoni \源\ 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對象不是null,但異常表明該對象可能爲空...此修復的任何線索或解決方案...?
好的...我將等待下一個版本......現在,我將回滾到v7.0.1,因爲代碼運行成功.... – user3658777