0

I am using Mvc.Net API with open XML. I need to replace multiple images in .docx file.I replace the images in current scenario but I don't get any Id or Name of the Image at my code side so facing difficulties to replace those images. Here is my code

List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
            {
                string Id=doc.MainDocumentPart.GetIdOfPart(imgPart);
                byte[] imageBytes = File.ReadAllBytes(ImagePath);
                BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
                writer.Write(imageBytes);
                writer.Close();
            }

Can I get the name of Image in ImagePart?

Shubham
  • 13
  • 4

1 Answers1

0

I would do something like this:

List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
           foreach(ImagePart imgPart in imgPartList)
                {
                    var imageId = document.MainDocumentPart.GetIdOfPart(imgPart.Id);
                    byte[] imageBytes = File.ReadAllBytes(ImagePath);
                    BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
                    writer.Write(imageBytes);
                    writer.Close();
                }

Also

This answer could help you

Eisenbiegler
  • 57
  • 1
  • 2
  • 12
  • Hi @Mr. R Thanks for your reply it helps me but my concern is The link you provided changed the images name I want the same image name for example if I added two images in docx User.jpg and UserId.jpg but at code side I got the image name like image1.jpg and image2.jpg instead of these changed name I want the original name so that I can replace the correct image, hope i am clear to you, Thanks in advance – Shubham Jul 03 '20 at 05:11
  • I think open xml changed the actual image name in word doc, is this so? – Shubham Jul 03 '20 at 05:30
  • I'm sorry, I can't tell you exactly. – Eisenbiegler Jul 03 '20 at 15:24
  • 1
    Thanks @Mr. R I achieved this goal thanks for share that link with your solution. – Shubham Jul 06 '20 at 04:27