I am trying to replace all the carrage returns ^l to a paragraph ^p in a word document and do not know how to go about doing that using the API. Any help would be great. Thanks
Ali.
I am trying to replace all the carrage returns ^l to a paragraph ^p in a word document and do not know how to go about doing that using the API. Any help would be great. Thanks
Ali.
Hi Ali,
Thank you for your request. This task is not quite straightforward because line breaks in the Aspose.Words model are control chars (i.e. contained in run’s text) but paragraphs are separate nodes. So the best option here is using replace evaluator:
[Test]
public void TestReplaceLineBreak()
{
Document doc = TestUtil.Open(@"TestReplaceLineBreak.doc");
doc.Range.Replace(new Regex(@"\v"), new ReplaceEvaluator(MyEvaluator), false);
TestUtil.Save(doc, @"TestReplaceLineBreak Out.doc");
}
private ReplaceAction MyEvaluator(object sender, ReplaceEvaluatorArgs e)
{
DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);
builder.MoveTo(e.MatchNode);
string text = e.MatchNode.GetText();
string textBefore = text.Substring(0, e.MatchOffset);
string textAfter = text.Substring(e.MatchOffset + 1, text.Length - e.MatchOffset - 1);
builder.Write(textBefore);
builder.Writeln();
builder.Write(textAfter);
e.MatchNode.Remove();
return ReplaceAction.Skip;
}
Please let me know if it works.
Hi Dmitry,
Thanks a bundle. It worked great.
Thanks again.
Ali Naqvi