jeff_ang24
jeff_ang24

Reputation: 129

Document failed adding Table object on iText 7.0.2

I recently upgraded my iText7 project from 7.0.1 to 7.0.2. After that, I tried to rebuild my project and did the unit test. The unit test gave me an error, then I tried to debug the unit test. The result is there is a weird exception that I never found before... The code is same, but it run smoothly on v7.0.1, but below exception was caught on 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

Below is my table generator code:

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);

Below is my _Core_SetIdentityTableValue function:

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;
}

Is this a bug from v7.0.2 since v7.0.1 is successfully running the above code...? I also checked that the Table object is not null, but the exception tells that the object may be null... Any clue or solution for this fix...?

Upvotes: 0

Views: 408

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12302

Unfortunately this is indeed a bug in 7.0.2 which is related to the incorrect processing of relative positioning for tables and is present in both Java and C#. It has already been fixed, but the fix will only be available in 7.0.3 release. However, you can always download the snapshot version (also known as 7.0.2.1 in C#) from the Artifactory.

Upvotes: 1

Related Questions