How to find the text and replace it with image on Title Page of the Word document?
I have a template Title page with pattern {QrCode} and I want to paste there the image of generated QR code.
When Im using this solution How to find the text and replace it with image using .NET - #9 by tahir.manzoor solution, I don`t get what I want
Thanks!
@cagecrew You can use this code to replace text to image:
Document doc = new Document("input.docx");
byte[] image = File.ReadAllBytes("Barcode.png");
ReplaceWithImageHandler findElem = new ReplaceWithImageHandler(image);
FindReplaceOptions opt = new FindReplaceOptions()
{
ReplacingCallback = findElem,
Direction = FindReplaceDirection.Backward,
};
doc.Range.Replace("{QrCode}", string.Empty, opt);
doc.Save("output.docx");
public class ReplaceWithImageHandler : IReplacingCallback
{
private readonly byte[] _image;
public ReplaceWithImageHandler(byte[] image)
{
_image = image;
}
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
{
DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document);
builder.MoveTo((Run)args.MatchNode);
builder.InsertImage(_image);
return ReplaceAction.Replace;
}
}