简体   繁体   English

如何使用 C#.Net 通过 Microsoft.Interop.Word 比较 word 文档每一页中存在的图像(形状)?

[英]How to compare image (Shape) present in each page of word document through Microsoft.Interop.Word using C#.Net?

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)我正在使用以下代码将 word 文档的图像(Microsoft.Interop.Office.Word 中的形状)替换为新图像,但客户的要求是我需要检查 word 文档第一页的第一张图像和然后将此图像与文档的 rest 的图像进行比较,如果匹配,则将其替换为新图像,否则不需要帮助我们如何比较两个形状(图像)

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;

                                    }
}

The Word object model doesn't provide anything to compare two images. object model 这个词没有提供任何东西来比较两个图像。 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.答案是Open XML SDK ,它允许即时获取图像的字节表示,而无需之前将它们保存到磁盘。 The Open XML SDK contains a class WordprocessingDocument that can manipulate a memory stream containing a WordDocument content. 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[] .并且MemoryStream可以使用ToArray()转换为byte[] See Convert Word of interop object to byte [] without saving physically for more information.有关详细信息,请参阅将 interop object 的字转换为字节 [] 而无需物理保存

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Microsoft.Interop.Word在Excel文档中嵌入Excel文件? - How to embed an excel-file in a word-document using Microsoft.Interop.Word? 如何使用C#Interop检查形状/图像在Word文档的哪一页上 - How to check on which page of a word document a shape/image is located using C# Interop 如何使用microsoft.interop.word从Word文件中读取项目符号列表? - How to read bullet list from word file using microsoft.interop.word? 如何使用Microsoft.Office.Interop.Word C#在Word文档中插入/获取封面 - How to insert/fetch a cover page in word document using Microsoft.Office.Interop.Word C# 使用Microsoft Word.interop删除Word文档中的空白页 - Blank page removal in a word document using microsoft word.interop 如何使用C#.net编辑和打印word或pdf文档? - How to edit and print a word or pdf document using C#.net? 使用 Word.Interop 出现 åäö 错误:查找替换 c#.Net - ERROR with åäö using Word.Interop: Find Replace c#.Net 使用.Net中的Microsoft.Office.Interop.Word将文档导出为HTML? - Export document as HTML using Microsoft.Office.Interop.Word in .Net? 如何使用 Word Interop 识别 Word 文档中每页的最后一段? - How to identify last Paragraph of each page in a Word document using Word Interop? 如何在C#中使用Microsoft.Office.Interop.Word从Word文件(.Docx)中按页获取文本 - How to get text by page from word file (.Docx) using Microsoft.Office.Interop.Word in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM