Exception in FieldsHelper

Hi
We have Aspose.Words 14.2, and FieldsHelper is used to replace IFs with static texts, which worked fine. However, in a recent case, the code threw an exception from the following highlighted line.
My questions are,

  1. We know that FieldsHelper is not part of Aspose.Words. But is there an available fix in the helper class? The code should handle the case when the nextParagraph is null.
  2. If not, is there an equivalent built-in feature in the latest Aspose.Words, which is more robust?

Please advise!

function call:

FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldIf);

Exception thrown from below:

public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
{
    if (mFieldDepth > 0)
    {
        // The field code that is being converted continues onto another paragraph. We
        // need to copy the remaining content from this paragraph onto the next paragraph.
        Node nextParagraph = paragraph.NextSibling;
        // Skip ahead to the next available paragraph.
        while (nextParagraph != null && nextParagraph.NodeType != NodeType.Paragraph)
            nextParagraph = nextParagraph.NextSibling;
        // Copy all of the nodes over. Keep a list of these nodes so we know not to remove them.
        while (paragraph.HasChildNodes)
        {
            mNodesToSkip.Add(paragraph.LastChild);
            ((Paragraph)nextParagraph).PrependChild(paragraph.LastChild); //<== nextParagraph is somehow Null!!**
        }
        paragraph.Remove();
    }
    return VisitorAction.Continue;
}

Regards,

Jason

Hi Jason,

Thanks for your inquiry. Please upgrade to the latest version of Aspose.Words for .NET (16.1.0) and see how it goes on your end. Hope, this helps.

In case the problem still remains, please zip and attach your template Word document here for testing. We will investigate the issue on our end and provide you more information.

Best regards,

Hi Awais,

We just tried the trail version of the latest DLL, and the same issue still occurs. Attached is the template that we use.

Regards,

Jason

Hi Jason,

Thanks for your inquiry. We managed to reproduce this exception (System.NullReferenceException) on our end. We are working on this issue further and will get back to you soon.

Best regards,

Glad to hear that! Thank you Awais! I hope that you’d have a fix soon in the FieldsHelper rather than in the Aspose.Words.

Hi Jason,

Thanks for being patient. Please add the following expression in while loop:

public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
{
    if (mFieldDepth > 0)
    {
        // The field code that is being converted continues onto another paragraph. We 
        // need to copy the remaining content from this paragraph onto the next paragraph.
        Node nextParagraph = paragraph.NextSibling;
        // Skip ahead to the next available paragraph.
        while (nextParagraph != null && nextParagraph.NodeType != NodeType.Paragraph)
            nextParagraph = nextParagraph.NextSibling;
        // Copy all of the nodes over. Keep a list of these nodes so we know not to remove them.
        while (paragraph.HasChildNodes && nextParagraph != null)
        {
            mNodesToSkip.Add(paragraph.LastChild);
            ((Paragraph)nextParagraph).PrependChild(paragraph.LastChild);
        }
        paragraph.Remove();
    }
    return VisitorAction.Continue;
}

Hope, this helps.

Best regards,

Thanks Awais for the reply. Can you please elaborate on the logic of the fix? The code fix does prevent the exception from happening in this particular case. But I doubt that it is the correct fix. Because when the next paragraph is null, meaning the current paragraph is the last in the current context, why the paragraph is removed even it contains child paragraph?

Regards,

Jason

Hi Jason,

Thanks for your inquiry. We are in coordination with product team to get answer pertaining to your queries. Soon you will be updated with the required information.

Best regards,

Hi Jason,

Thanks for being patient. The problem occurs because there is Bookmark inside table which is inside field code. All that you need to do is override the VisitBookmarkStart and VisitBookmarkEnd in the same way as VisitRun. You do not need to change VisitParagraphEnd code in this case:

public override VisitorAction VisitBookmarkStart(BookmarkStart bookmarkStart)
{
    CheckDepthAndRemoveNode(bookmarkStart);
    return VisitorAction.Continue;
}
public override VisitorAction VisitBookmarkEnd(BookmarkEnd bookmarkEnd)
{
    CheckDepthAndRemoveNode(bookmarkEnd);
    return VisitorAction.Continue;
}

Hope, this helps.

Best regards,

Thank you Awais! This fix works fine.

HI Awais,

We just saw another exception thrown from the same line of code for a different user document. And this time is the fact that an image is used inside a field code. So we patched the code to override VisitDrawingML() using the same pattern that you provided, and it worked. Because there’re over 40 visit methods that can be overridden, do you have a guideline on how many of them should be overridden in the same way? Please advise!

public override VisitorAction VisitDrawingML(DrawingML drawingMl)
{
    this.CheckDepthAndRemoveNode(drawingMl);
    return base.VisitDrawingML(drawingMl);
}

Regards,

Jason

Hi Jason,

Thanks for your inquiry. Please note that VisitDrawingML method in DocumentVisitor was replaced with VisitDrawingMLStart and VisitDrawingMLEnd in Aspose.Words 14.9.0.

We did some more changes in Drawing APIs. DocumentVisitor.VisitDrawingMLStart and VisitDrawingMLEnd were removed from Aspose.Words 15.2.0. You need to replace them with VisitShapeStart/VisitShapeEnd in your code. Please check the detail of API changes from following link.
Public API Changes in Aspose.Words 15.2.0

If you still face problem, please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

Thank you Tahir. Here’s the doc which causes the issue. Please advise the fix, and what else we could do to improve the quality of FieldsHelper, which is not part of Aspose.Words. BTW, we’ve tested the latest version 15.2.0, and the problem is also there.

Regards,

Jason

Hi Jason,

Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Hi Awais,

Can you provide an update on this matter? Have you had a chance to triage this issue? Can you confirm whether this is an issue? If so, do you have an estimated deliver date?

Regards,

Jason

Hi Jason,

Thanks for your inquiry. We have simplified your document such that the same exception is still reproducible (see attachment). This is absolutely different exception and this exception says “System.InvalidOperationException : Cannot remove because there is no parent.”
So it means that the parent node was removed previously and now its child node cannot be removed. You can bypass this exception by doing the following changes in code:

public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
{
    if (mFieldDepth > 0)
    {
        // The field code that is being converted continues onto another paragraph. We 
        // need to copy the remaining content from this paragraph onto the next paragraph.
        Node nextParagraph = paragraph.NextSibling;
        // Skip ahead to the next available paragraph.
        while (nextParagraph != null && nextParagraph.NodeType != NodeType.Paragraph)
            nextParagraph = nextParagraph.NextSibling;
        // Copy all of the nodes over. Keep a list of these nodes so we know not to remove them.
        while (paragraph.HasChildNodes)
        {
            mNodesToSkip.Add(paragraph.LastChild);
            ((Paragraph)nextParagraph).PrependChild(paragraph.LastChild);
        }
        if (paragraph.ParentNode != null)
            paragraph.Remove();
    }
    return VisitorAction.Continue;
}

And

private void CheckDepthAndRemoveNode(Node node)
{
    if (mFieldDepth > 0 && !mNodesToSkip.Contains(node) && node.ParentNode != null)
        node.Remove();
}

Hope, this helps.

Best regards,