How to insert HTML at specific position?

Hello,
How can I insert a string containing HTML into a Microsoft Word document at a specific position? I am aware of the function “InsertHTML”, but the API does not specify where it inserts the HTML.
What I am trying to achieve is the following:
I need to open a Microsoft Word document, which contains special keywords, like “[$Paragraph1]” and so on. Those keywords need to be replaced with some HTML. The function “Replace” can only replace plain text (according to the API), so I need to use the function “InsertHTML”.
Could someone please give an example of how I can achieve this?
Many thanks!

Yes, that can be easily done using the ReplaceEvaluator:

public void ReplaceWithInsertHtml(Document doc)
{
    doc.Range.Replace(new Regex(@"\[\$Paragraph1\]"), new ReplaceEvaluator(ReplaceWithHtmlEvaluator), false);
}
private ReplaceAction ReplaceWithHtmlEvaluator(object sender, ReplaceEvaluatorArgs e)
{

    DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);
    builder.MoveTo(e.MatchNode);
    builder.InsertHtml("<b>Some html.</b> <i>More html.</i>");
    e.Replacement = "";
    return ReplaceAction.Replace;
}

Thanks for a good question. I will add this example to InsertHtml API Reference.
Best regards,

Many thanks for your quick solution! It works just perfectly.

I just tried to do this and it doesn’t seem to work at all for me. Thoughts? Here is my Evaluator:

Private Shared Function DocEvaluator(ByVal sender As Object, ByVal e As Aspose.Words.ReplaceEvaluatorArgs) As Aspose.Words.ReplaceAction
Dim builder As New Aspose.Words.DocumentBuilder(e.MatchNode.Document)
builder.MoveTo(e.MatchNode)
builder.InsertHtml("<span class=""This_Is_A_Match"">")
e.Replacement = ""
Return Aspose.Words.ReplaceAction.Replace
End Function

If I search in my output, This_Is_A_Match is never found. But the code absolutely runs cause my breakpoint there gets hit. Ideas?

I am using the demo version, version 5.0.0.0.

Hi
Thanks for your inquiry. Could you please attach your document for testing and provide me your Regex. Maybe your document is truncated. (Evaluation version of Aspose.Words limits the maximum document size to several hundred paragraphs).
Best regards.

The document definitely isn’t being truncated. It’s just a 1 line document. So for example, here is the content of my document:

This is a <%test%> of regex replace

And this is my regex

Dim regularExpression As New System.Text.RegularExpressions.Regex("<%[\w. -]+%>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

And the resulting output is:

Evaluation Only. Created with Aspose.Words. Copyright 2003-2008 Aspose Pty Ltd.
This is a of regex replace

Hi
Thanks for additional information. It seems that all works properly. In fact is empty string. Try using another HTML string and I believe that you will get properly result. For example try using the following HTML string.
Some text here
Best regards.

Ok, got 2 different problems now.

  1. If I use:
builder.InsertHtml("<span style=""color: red;"">" + e.Match.Value + "")

Then my output looks like:

Evaluation Only. Created with Aspose.Words. Copyright 2003-2008 Aspose Pty Ltd.
<%test%>This is a of regex replace

Where the inserted text is in the wrong place.

  1. If I use:
builder.InsertHtml("" + e.Match.Value + "")

and I save my output out to HTML, I don’t see the inserted span in the output html.

Any thoughts on either of these?

Hi

  1. Please try using the following code snippet.
ReplaceAction EvaluateReplaceTags(object sender, ReplaceEvaluatorArgs e)
{
    // Get MatchNode
    Run run1 = (Run)e.MatchNode;
    // Create Run
    Run run2 = new Run(e.MatchNode.Document);
    // Get index of match value
    int index = run1.Text.IndexOf(e.Match.Value);
    if (index > 0)
    {
        // Split text in match node
        run2.Text = run1.Text.Substring(0, index);
        run1.Text = run1.Text.Substring(index + e.Match.Value.Length);
    }
    run1.ParentParagraph.InsertBefore(run2, run1);
    DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);
    // Move to run1
    builder.MoveTo(run1);
    // Insert HTML
    builder.InsertHtml("<b>Test</b>");
    return ReplaceAction.Skip;
}
  1. The main thing is that Aspose.Words HTML import and export does not guarantee full data roundtrip. See the following document for more information.
    https://docs.aspose.com/words/net/supported-document-formats/

Best regards.

Yeah, that appears to be working better. Thanks for the new code snippet.

Sorry, I spoke too soon. It’s not working after all.

Here is the code I’m using for the replace (identical to yours, just VB-ified):

Private Shared Function EvaluateReplaceTags(ByVal sender As Object, ByVal e As Aspose.Words.ReplaceEvaluatorArgs) As Aspose.Words.ReplaceAction
'Get MatchNode
Dim run1 As Aspose.Words.Run = DirectCast(e.MatchNode, Aspose.Words.Run)

'Create Run
Dim run2 As New Aspose.Words.Run(e.MatchNode.Document)

'Get index of match value
Dim index As Integer = run1.Text.IndexOf(e.Match.Value)

If index > 0 Then
'Split text in match node
run2.Text = run1.Text.Substring(0, index)
run1.Text = run1.Text.Substring(index + e.Match.Value.Length)
End If
run1.ParentParagraph.InsertBefore(run2, run1)

Dim builder As New Aspose.Words.DocumentBuilder(e.MatchNode.Document)

'Move to run1
builder.MoveTo(run1)

'Insert HTML
builder.InsertHtml("**Test**")
Return Aspose.Words.ReplaceAction.Skip
End Function

Here is my regex:

Dim tokenRegularExpression As New System.Text.RegularExpressions.Regex("<%[\w. -]+%>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

Here is my input:

<%hamburger%>This is a <%taco%> of regex replace
This is anothier <%nacho%> of regex replace
And a third <%hotdog%> of regex replace
And here is my output:

Test<%hamburger%>TestThis is a <%taco%> of regex replace
TestThis is anothier <%nacho%> of regex replace
TestAnd a third <%hotdog%> of regex replace

Want to take another shot at it?

Thanks a lot

Hi
I tried the following code and all works fine on my side.

Sub Main()
Dim doc As Document = New Document("in.doc")
Dim tokenRegularExpression As New Regex("<%[\w\. -]+%>", RegexOptions.IgnoreCase)
doc.Range.Replace(tokenRegularExpression, New ReplaceEvaluator(AddressOf EvaluateReplaceTags), False)
doc.Save("out.doc")
End Sub
Private Function EvaluateReplaceTags(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction
'Get MatchNode
Dim run1 As Aspose.Words.Run = DirectCast(e.MatchNode, Aspose.Words.Run)
'Create Run
Dim run2 As New Aspose.Words.Run(e.MatchNode.Document)
'Get index of match value
Dim index As Integer = run1.Text.IndexOf(e.Match.Value)
If index > 0 Then
'Split text in match node
run2.Text = run1.Text.Substring(0, index)
End If
run1.Text = run1.Text.Substring(index + e.Match.Value.Length)
run1.ParentParagraph.InsertBefore(run2, run1)
Dim builder As New Aspose.Words.DocumentBuilder(e.MatchNode.Document)
'Move to run1
builder.MoveTo(run1)
'Insert HTML
builder.InsertHtml("<b>Test</b>")
Return Aspose.Words.ReplaceAction.Skip
End Function

I hope this could help you.
Best regards.

Just tried your code out with my sample input, and my output now is:

TestThis is a <%tacoTestegex replace
This is anothier <%nachoTestgex replace
And a third <%hotdogTestex replace

Any suggestions?

The previous post’s output was the result of the 3rd argument to the Replace method being True. If I set it to False, I get slightly different output:

TestTest a <%taco%> of regex replace
Testanothier <%nacho%> of regex replace
Testrd <%hotdog%> of regex replace

Hope that helps. Not sure it will though.

Hi
My code works fine on my side. Could you please attach your input document? I will investigate this and try to help you.
Best regards.

Doc is attached

Thanks

Hi
Thanks for your inquiry. There is some problem with your document. If you open your document using DocumentExplorer (Demo application) then you can see that your placeholders are represented as several runs. You can solve this problem using one more Replace evaluator. I think that the attached code could solve this problem.
Best regards.

That’s looking good so far. Thanks again for the help.