Word 自动化:使用 C# 替换图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1128873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:52:13  来源:igfitidea点击:

Word Automation: Replace an Image using C#

c#imageautomationms-wordreplace

提问by

I'm trying to change text and images in a word document using c# and word automation. I've got it working fine for text where I do something like the snippet below, but I don't even know how to start for replacing the image.

我正在尝试使用 c# 和 word 自动化更改 word 文档中的文本和图像。我在文本中可以正常工作,我在下面执行类似以下代码段的操作,但我什至不知道如何开始替换图像。

Any help is greatly appreciated!

任何帮助是极大的赞赏!

Oliver

奥利弗

using Microsoft.Office.Interop.Word;
...

private static Application WordApp;
private static object missing = System.Reflection.Missing.Value;
private static object yes = true;
private static object no = false;

...
object search;
object replace;

object replaceAll =
    Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

object filename = SourceFile;
object destination = DestinationFile;

Document d = WordApp.Documents.Open(
    ref filename, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing);

d.Activate();
search = "OLDSTRING";
replace = "NEWSTRING";
WordApp.Selection.Find.Execute(
    ref search, ref yes, ref yes, ref no, ref no, ref no, ref yes,
    ref missing, ref missing, ref replace, ref replaceAll,
    ref missing, ref yes, ref missing, ref missing);

回答by Travis

You can loop through the InlineShapes and replace the pictures

您可以遍历 InlineShapes 并替换图片

using System.Collections.Generic;
using Word = Microsoft.Office.Interop.Word;

namespace WordExample
{
    class WordExample
    {
        #region Constructor
        public WordExample()
        {
            WordApp = new Microsoft.Office.Interop.Word.Application();
        }
        #endregion

        #region Fields
        private Word.Application WordApp;
        private object missing = System.Reflection.Missing.Value;
        private object yes = true;
        private object no = false;
        private Word.Document d;
        private object filename = @"C:\FullPathToFile\example.doc";
        #endregion

        #region Methods
        public void UpdateDoc()
        {
            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.Range> ranges = new List<Microsoft.Office.Interop.Word.Range>();
            foreach (Word.InlineShape s in d.InlineShapes)
            {
                if (s.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
                {
                    ranges.Add(s.Range);
                    s.Delete();
                }
            }
            foreach (Word.Range r in ranges)
            {
                r.InlineShapes.AddPicture(@"c:\PathToNewImage\Image.jpg", ref missing, ref missing, ref missing);
            }
            WordApp.Quit(ref yes, ref missing, ref missing);
        }
        #endregion
 }
}

回答by Mario Favere

Do you want to replace a shape or inline shape ? This is a big difference!

您要替换形状还是内嵌形状?这是一个很大的不同!

For an inline shape there are plenty of examples on the net. For a shape you can do like this :

对于内联形状,网上有很多示例。对于形状,您可以这样做:

    private object missing = System.Reflection.Missing.Value;
    .....other code.....



        foreach (Microsoft.Office.Interop.Word.Shape s in wordApp.ActiveDocument.Shapes)
        {

            if (s.AlternativeText.ToUpper().Contains("FOTO"))
            {
                object A = s.Anchor;
                Shape new = Brief.Shapes.AddPicture(@"mynewpicture.jpg", ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref A);
                new.Top = s.Top;
                new.Left = s.Left;
                new.Width = s.Width;
                new.Height = s.Height;
                s.Delete();
            }
        }

回答by Vanda Ros

I just copy some path from @Mario Favere and make more easy

我只是从@Mario Favere 复制了一些路径,让它变得更容易

  • First create Blank Picture and Insert Picture into word Document
  • 首先创建空白图片并将图片插入到word文档中

enter image description here

在此处输入图片说明

  • Second need to add Alt Text right click on Picture
  • 第二个需要在图片上右键添加Alt Text

enter image description here

在此处输入图片说明

private object missing = System.Reflection.Missing.Value;
.....other code.....

// Change Image

foreach (Microsoft.Office.Interop.Word.Shape s in wordApp.ActiveDocument.Shapes)
{

    if (s.AlternativeText.ToUpper().Contains("POTO"))
    {
        s.Fill.UserPicture(@"PATH");                       
    }
}