Hi,
Can someone please post the piece of replaceevaluator code for replacing the text in a word document with a custom image from a path?
I need C# dotnet version only…
Thanks,
Yashwanth
Hi,
Can someone please post the piece of replaceevaluator code for replacing the text in a word document with a custom image from a path?
I need C# dotnet version only…
Thanks,
Yashwanth
Hi
Yashwanth,
Thanks for your inquiry. I think, you can achieve what you need by using the following code snippet:
Document doc = new Document(@"C:\temp\in.docx");
doc.Range.Replace(new Regex(@"text to
replace"), new ReplaceWithImageEvaluator(), false);
doc.Save(@"C:\temp\out.docx");
private class ReplaceWithImageEvaluator : IReplacingCallback
{
///
/// NOTE: This is a simplistic method that will only work well when the match
/// starts at the beginning of a run.
///
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
builder.MoveTo(e.MatchNode);
// Replace ‘text to replace’ text with an image.
Shape img = builder.InsertImage("http://www.aspose.com/images/aspose-logo.gif");
e.Replacement = "";
return ReplaceAction.Replace;
}
}
I hope, this will work.
Best Regards,
Thanks a lot for this info this worked in a nice manner.
But only one problem with this is if we put some thing like
ImageToken: [#ImageToken#]
Say if i give like this where [#ImageToken#] should be replaced with the image then the problem here is the image is coming first and the text that is there before this "ImageToken: " is comming after the image where it should be opposite.
Any help regarding this is greatly appreciatable.
–Yashwanth
Hi Yashwanth,
Thanks for your inquiry. Please try passing the following regular expression and let us know how it goes:
doc.Range.Replace(new Regex(@"</span>[#ImageToken#</span>]"),
new ReplaceWithImageEvaluator(), false);
I hope, this will help.
Best Regards,