Free Support Forum - aspose.com

Insert html at desired point

Hi

Im evaluating your product and there is only one thing stopping us going ahead at the moment, i have implemented search and replace but when i wish to insert html into the exact point that i find my tag, it either inserts at the beginning of the paragraph or end, and feeding html into the Replacement only renders the html as text...

i am using

//Replace tags matches

doc.Range.Replace(new System.Text.RegularExpressions.Regex(CurlyBracketsTagExpression),

new Aspose.Words.ReplaceEvaluator(EvaluateReplaceTags), false);

then

private ReplaceAction EvaluateReplaceTags(object sender, ReplaceEvaluatorArgs e)

{

Paragraph para = (Paragraph)e.MatchNode.ParentNode;

DocumentBuilder b = new DocumentBuilder(para.Document);

b.MoveTo(e.MatchNode);

b.InsertHtml("Test");

e.Replacement = String.Empty;

return ReplaceAction.Replace;

}

When is put in a line like : This is a {tag:test} and another tag {tag:test2} i get this problem.

How do you insert at the required offset within a node, ie (MatchOffset) ?

if i do doc.MoveTo(e.MatchNode) the insertHtml it goes at the front, not at the offset i require...?

Hi

Thanks for your inquiry. 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("Test");

builder.InsertBreak(BreakType.ParagraphBreak);

return ReplaceAction.Skip;

}

I hope this could help you.

Best regards.

Works a treat, thanks!

Will be purchashing soon.