[英]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.