Write Numbered or Bulleted List in Smart Tags of Word Document & Preserve List Numbering MERGEFIELD C# .NET

Updating merge fields that are part
of a numbered or bulletted list with text that has line breaks does not continue the list. Say you have a document with a list
that looks like:

1: some text
2: some text
2.1 some more text
2.2 <>

in word updating <> with text that has line breaks in it causes the list to continue like this:

2.2 merge field replaced text
2.3 merge field replaced text after line break

with aspose it looks like

2.2 merge field replaced text
merge field replace text after line break

The list does not continue at 2.3 with aspose where the line break is. How can I get around this?

Thanks,

Adam

Related to this, when a mergefield does span multiple paragraphs your MergeField class does not handle it correctly, so updating the field does not work properly. The constructor has this code:

internal MergeField(FieldStart fieldStart)
{
    if (fieldStart.Equals(null))
    {
        throw new ArgumentNullException("fieldStart");
    }
    if (!fieldStart.FieldType.Equals(FieldType.FieldMergeField)
    && !fieldStart.FieldType.Equals(FieldType.FieldTime)
    && !fieldStart.FieldType.Equals(FieldType.FieldNone))
    {
        throw new ArgumentException("Field start type must be FieldMergeField or FieldTime or FieldNone.");
    }
    mFieldStart = fieldStart;

    // Find the field separator node.
    mFieldSeparator = FindNextSibling(FieldStart, NodeType.FieldSeparator);

    if (mFieldSeparator == null)
    {
        throw new InvalidOperationException("Cannot find field separator.");
    }

    // Find the fieldend node. Normally fieldend will always be found, but in the example document
    // there happens to be a paragraph break included in the hyperlink and this puts the fieldend
    // in the next paragraph. It will be much more complicated to handle fields which span several
    // paragraphs correctly, but in this case allowing fieldend to be null is enough for our purposes.
    mFieldEnd = FindNextSibling(mFieldSeparator, NodeType.FieldEnd);

…

in these cases, the merge field spans several paragraphs so mFieldEnd does not get set correctly which means the field can not be updated properly. Do you have code that can set the field end properly?

Thanks,

Adam

Hi
Thanks for you request. This code is just an example that demonstrates the technique. You are free to change this code.
I think multi-paragraph merge fields do not occur very often in the real life. Also I think technique you are using to populate your document with data is very error prone.
Regarding inserting paragraphs, I think you can try using DocumentBuilder.WriteLine method.
Best regards.

Thanks for your response.

The way we are populating the data might be very error prone with Aspose but it worked fine with Word and this is what we are now stuck with as 20+ clients using our software are not going to each change hundreds of documents so that aspose works better.

Regarding using DocumentBuilder.WriteLine method,

we are setting the value of the mergefield to a block of text that contains the line feeds using code like this:

theField.Value = text;

How would we use DocumentBuilder.WriteLine to insert paragraphs?

Thanks,

Adam

Hi Adam,
I modified code of MergeField class. Now it should work fine. Please see the attachment.
Best regards.

Thank you very much this is extremely helpful.

Alexey,

This code works great in most situations but in some situations when setting the value using this code:

internal string Value
{
    get
    {
        return GetTextFromSequence(mFieldSeparator, mFieldEnd);
    }
    set
    {
        // Delete runs that represents field code
        RemoveSequence(mFieldSeparator, mFieldEnd);
        // Also if field separator and field end have different parents, 
        // we should insert field end directly after FieldSeparator
        if (mFieldSeparator.ParentNode != mFieldEnd.ParentNode)
        {
            // Get parent node of field end
            CompositeNode parent = mFieldEnd.ParentNode;
            mFieldSeparator.ParentNode.InsertAfter(mFieldEnd, mFieldSeparator);

            // If there is no more chield nodes in parent nore we should remove it
            if (!parent.HasChildNodes)
                parent.Remove();
        }

        mBuilder.MoveTo(mFieldEnd);
        mBuilder.Write(value);
    }
}

We get an exception: “The node must be a paragraph or a direct child of a paragraph.” at the mBuilder.MoveTo(mFieldEnd) call. Any ideas?

Thanks,

Adam

Hi Adam,
Thanks for your request. Could you please provide me sample document? I will try to reproduce the problem on my side and provide you more information.
Best regards.

Thanks for your quick response. I have attached the document. The error seems to be occurring with the
MERGEFIELD RESWARE_SP_GetPropertyCounty_1 field.

Adam

Hi Adam,
Thank you for additional information. The problem occurs because field is inside a SmartTag. I created new issue #7432 in our defect database regarding this problem. I will notify you as soon as it is fixed.
Best regards.

The issues you have found earlier (filed as 7432) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by alexey.noskov.