Free Support Forum - aspose.com

Is there a markup for making styling text (making italics) in Aspose.Words?

I’ve got a program which is inserting some text into a Word document. The text comes from the code, so it’s plain ascii text. It’s inserted into the Word document using a objWord.Range.Replace method. I can’t call any additional methods or access the word doc object’s properties without refactoring code significantly, so what I’m looking for is something equivalent to objWord.Range.Replace(“string to find”,“replacement string”), but where the tag is instead something that MS Word will render. I suppose Word uses binary weirdness to do markup, so I don’t know if this is possible. Any advice is appreciated.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think that you should use ReplaceEvaluetor to achieve this. For example see the following code.

public void Test091()

{

Document doc = new Document(@"Test091\in.doc");

Regex regex = new Regex("string to find");

//This is needed because search pattern can be represented as several runs

//after replacing it will be represented as one run

doc.Range.Replace(regex, "string to find");

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceEvaluator091), false);

doc.Save(@"Test091\out.doc");

}

private ReplaceAction ReplaceEvaluator091(object sender, ReplaceEvaluatorArgs e)

{

//Get MatchNode

Run firstRun = (Run)e.MatchNode;

//Create Run

Run secontRun = new Run(e.MatchNode.Document, string.Empty);

//Get index of match value

int index = firstRun.Text.IndexOf(e.Match.Value);

if (index > 0)

{

//Split text in match node

secontRun.Text = firstRun.Text.Substring(0, index);

firstRun.Text = firstRun.Text.Substring(index + e.Match.Value.Length);

}

else

{

firstRun.Text = firstRun.Text.Replace(e.Match.Value, "");

}

firstRun.ParentParagraph.InsertBefore(secontRun, firstRun);

DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);

//Move to first run

builder.MoveTo(firstRun);

//Insert html

builder.InsertHtml("replacement string");

return ReplaceAction.Skip;

}

I hope this could help you.

Best regards.