This is a migrated thread and some comments may be shown as answers.

Replacing an image in the document

1 Answer 196 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Kayani
Top achievements
Rank 1
Kayani asked on 04 Mar 2020, 04:36 PM

 

Hello,

I want to open a docx, find and replace an image in the header and replace another image in the body. How can I achieve that?

Thanks

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 05 Mar 2020, 12:28 PM

Hi Waqar,

You can use the following approach for this:

DocxFormatProvider provider = new DocxFormatProvider(); RadFlowDocument document; using (FileStream fs = new FileStream(@"..\..\test1.docx", FileMode.Open)) { document = provider.Import(fs); } var header = document.Sections.First().Headers.Default; var headeImages = header.EnumerateChildrenOfType<ImageInline>(); var images = document.EnumerateChildrenOfType<ImageInline>();

foreach (var item in images) { if (headeImages.Contains(item)) { item.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(File.ReadAllBytes(@"../../green_tick.png"), "png"); } else { item.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(File.ReadAllBytes(@"../../red_x.png"), "png"); } } using (Stream output = new FileStream(@"..\..\output.docx", FileMode.OpenOrCreate)) { provider.Export(document, output); } 

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
WordsProcessing
Asked by
Kayani
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or