簡體   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?

我正在使用以下代碼將 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;

                                    }
}

object model 這個詞沒有提供任何東西來比較兩個圖像。 您可以做的最好的事情是將兩者都保存在磁盤上,然后嘗試比較兩者的字節表示。 但是,有更好的方法來完成工作。 答案是Open XML SDK ,它允許即時獲取圖像的字節表示,而無需之前將它們保存到磁盤。 The Open XML SDK contains a class WordprocessingDocument that can manipulate a memory stream containing a WordDocument content. 並且MemoryStream可以使用ToArray()轉換為byte[] 有關詳細信息,請參閱將 interop object 的字轉換為字節 [] 而無需物理保存

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM