0

I am using following code to replace image (Shape in Microsoft.Interop.Office.Word) of the word document with new image but what the requirement from client is that I need to check the 1st Image of the 1st page of the word document and then compare this image with image of the rest of the document and if match it get replaced with new image else not so need help on how can we compare two shapes(Images)

public void ReplaceWordImage(string FilePath)
        {
            Word.Document d = new Word.Document();
            Word.Application WordApp;
            WordApp = new Microsoft.Office.Interop.Word.Application();
            bool headerImage = false;
            try
            {

                object missing = System.Reflection.Missing.Value;
                object yes = true;
                object no = false;
                object filename = @"D:/ImageToReplace/5.docx";


                d = WordApp.Documents.Open(ref filename, ref missing, ref no, ref missing,
                   ref missing, ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref yes, ref missing, ref missing, ref missing, ref missing);
                List<Word.ShapeRange> ranges = new List<Microsoft.Office.Interop.Word.ShapeRange>();
                List<Word.ShapeRange> headerRanges = new List<Microsoft.Office.Interop.Word.ShapeRange>();

  foreach (Word.Shape shape in d.Shapes)
                        {
                            if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoPicture)
                            {
shape.Delete();

foreach (Word.Range r in ranges)
                                `enter code here`    {
                                        r.InlineShapes.AddPicture(@"D:\Untitled.jpg", ref missing, ref missing);
                                        break;

                                    }
}

1 Answers1

1

The Word object model doesn't provide anything to compare two images. The best what you could do is to save both on the disk and then try comparing the bytes representation of both. However, there is a better way to get the job done. The answer is the Open XML SDK which allows getting the bytes representation of images on the fly without saving them to a disk before. The Open XML SDK contains a class WordprocessingDocument that can manipulate a memory stream containing a WordDocument content. And MemoryStream can be converted using ToArray() to a byte[]. See Convert Word of interop object to byte [] without saving physically for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Neither of these approaches is reliable, since the images as rendered in the document -which is independent of how they're stored in the XML - might be scaled or cropped differently. A better approach to ensuring a series of images is the same is to insert in once, bookmark it, then cross-reference the bookmark wherever else the image is needed. Additionally, if the image is in a given page header/footer (there are 3 per section), the copy that appears on each page must be the same unless field coding is used to manipulate with images are shown on different pages. – macropod Jun 01 '21 at 00:31
  • @Eugent:- Word document is already exist with 20-25 images and need to replace only logo image which might or might not present in every page of the document but always present in first page of the document. Users added it separately and not part of header so even if use openxml it seems we need to provide path of file name like byte[] templateContent = File.ReadAllBytes(templateFile); – Vaishali Khandelwal Jun 01 '21 at 05:43
  • So in above situation does we need to first download all the images of the file and convert into byte stream and check if they are equal OR will have to first convert logo image to byte array and then complete document to byte array and then compare the document byte array with image bytearray. even if we do that can we simply replace that amount of byte array with other image byte array. kindly correct if my understanding is different then what you actually want to suggest. – Vaishali Khandelwal Jun 01 '21 at 05:48
  • Yes goal is to compare all images of that document with 1st image of that document and replace 1st and all those images which match with 1st image with some other image as it is a logo image – Vaishali Khandelwal Jun 01 '21 at 05:49
  • There is no need to convert the whole document. It illustrates the power of Open XML SDK, so you may benefit from using it. But that road is out of my knowledge scope. As for the Word object model - you will have to save an image as a file, get the bytes to be able to compare them to others. – Eugene Astafiev Jun 01 '21 at 15:27