We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to create Text From Field by InsertHtml()?

Hello,

I found this method to create Text From Field:
builder.InsertField(@“MERGEFIELD MyFieldName * MERGEFORMAT”);

But I need to create Field from HTML.

I have, for example, this HTML:

var content = "<div><p>Lorem ipsum dolor sit amet, {{My Text Field 1}} consectetur adipiscing elit. Amet, pellentesque id nisi. Nullam a sodales enim {{My Text Field 2}}. Pellentesque at sodales lacus. Phasellus non tincidunt neque.</p></div>";

content = Regex.Replace(text, @"{{(.*?)}}",
                     m => {
                           return "<????>" + m.ToString() + "</????>";
                          });

builder.InsertHtml("<html><body>" + content + "</body></html>");

What tag can I replace {{…}} with to get Field in Word?

Maybe I can do something after .InsertHtml() method was run?

Thank you!

Hi Denis,

Thanks for your inquiry.

I’m afraid fields aren’t imported from HTML as there isn’t any equivalent HTML element to represent them.

However you can replace any tags inserted from your HTML with fields by using code similar to the article here.
https://docs.aspose.com/words/net/find-and-replace/

In this case instead of highlighting the runs you would want to insert a field as shown below.

// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
    SplitRun((Run)currentNode, remainingLength);
    runs.Add(currentNode);
}
DocumentBuilder builder = new DocumentBuilder(currentNode.Document);
builder.MoveTo(currentNode);
builder.InsertField(args.Match.Value);
// Now remove all runs in the sequence.
foreach (Run run in runs)
    run.Remove();

If we can help with anything else, please feel free to ask.

Thanks,

Thank you for your answer!

Now I am trying to use your example with highlighting for my case.

I have a very simple document:

Your address: [ADDRESS]
You
name: [NAME]

My code:

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
    // This is a Run node that contains either the beginning or the complete match.
    Node currentNode = e.MatchNode;

    // The first (and may be the only) run can contain text before the match, 
    // in this case it is necessary to split the run.
    if (e.MatchOffset > 0)
        currentNode = SplitRun((Run)currentNode, e.MatchOffset);

    if (currentNode != null)
    {
        DocumentBuilder builder = new DocumentBuilder((Document)currentNode.Document);
        builder.MoveTo(currentNode);
        builder.InsertTextInput("TextInput", TextFormFieldType.Regular, string.Empty, "MY FIELD", 0);
    }

    // This array is used to store all nodes of the match for further highlighting.
    ArrayList runs = new ArrayList();

    // Find all runs that contain parts of the match string.
    int remainingLength = e.Match.Value.Length;
    while (
    (remainingLength > 0) &&
    (currentNode != null) &&
    (currentNode.GetText().Length <= remainingLength))
    {
        runs.Add(currentNode);
        remainingLength = remainingLength - currentNode.GetText().Length;

        // Select the next Run node. 
        // Have to loop because there could be other nodes such as BookmarkStart etc.
        do
        {
            currentNode = currentNode.NextSibling;
        }
        while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
    }

    // Split the last run that contains the match if there is any text left.
    if ((currentNode != null) && (remainingLength > 0))
    {
        SplitRun((Run)currentNode, remainingLength);
        runs.Add(currentNode);
    }

    // Removes all matched runs
    foreach (Run run in runs)
    {
        run.Remove();
    }
    return ReplaceAction.Skip;
}
}

Result:

Your address: MY FIELD
YouMY FIELD name: [NAME]

Problem:

As you see the first loop works well. I get MY FIELD at the right place. But second one comes to the wrong place. As I understand it happens because the document was changed after the first loop and that is why the second field is not placed correctly.

Can you help me with this?

And an additional question: How to change background color of text input field?

Hi there,

Thanks for this additional information.

Have you tried passing false to the third parameter of the Replace method (isForward). This will cause the replace to be done from the end of the document to the beginning which will hopefully fix your issue.

I’m afraid there is no such property for Microsoft Word form fields, you can only control shading by turning it on or off. This is controlled by the Document.ShadeFormData property.

Thanks,

Beautiful! You were 100% right. isForward was TRUE.

Document.ShadeFormData is what I was looking for!

Great support! Thank you!