字自动化:使用C#替换图像 [英] Word Automation: Replace an Image using C#

查看:242
本文介绍了字自动化:使用C#替换图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变使用C#和字自动化Word文档中的文本和图像。我知道了文本,我做类似下面的代码段工作正常,但我甚至不知道如何开始更换图像。

任何帮助是极大AP preciated!

奥利弗

 使用的Microsoft.Office.Interop.Word;
...私有静态应用WordApp;
私人静态对象缺少= System.Reflection.Missing.Value;
私人静态对象是= TRUE;
私人静态对象无= FALSE;...
对象搜索;
反对更换;对象的replaceAll =
    Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;目标文件名=的SourceFile;
对象目的地= DestinationFile;文档D = WordApp.Documents.Open(
    REF文件名,参考失踪,失踪参考,参考失踪,失踪参考,
    裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,
    裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,
    REF遗漏);d.Activate();
搜索=OLDSTRING;
取代=NEWSTRING;
WordApp.Selection.Find.Execute(
    文献检索,文献是的,裁判是的,裁判没有,没有裁判,裁判没有,裁判是的,
    裁判失踪,失踪参考,楼盘更换裁判的replaceAll,
    裁判失踪,裁判是的,裁判失踪,失踪参考);


解决方案

您可以通过InlineShapes循环和更换图片

 使用System.Collections.Generic;
使用Word =的Microsoft.Office.Interop.Word;命名空间WordExample
{
    类WordExample
    {
        #区域构造
        公共WordExample()
        {
            WordApp =新Microsoft.Office.Interop.Word.Application();
        }
        #endregion        #地区字段
        私人Word.Application WordApp;
        私有对象缺少= System.Reflection.Missing.Value;
        私有对象是= TRUE;
        私有对象无= FALSE;
        私人Word.Document D组;
        私人目标文件名= @C:\\ FullPathToFile \\ example.doc
        #endregion        #区域方法
        公共无效UpdateDoc()
        {
            D = WordApp.Documents.Open(REF文件名,REF缺失,没有参考,参考失踪,
               裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,
               REF缺失,缺少文献,文献是的,裁判失踪,失踪参考,参考失踪,失踪参考);
            清单< Word.Range>范围=新的List< Microsoft.Office.Interop.Word.Range>();
            的foreach(Word.InlineShape S IN d.InlineShapes)
            {
                如果(s.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
                {
                    ranges.Add(s.Range);
                    s.Delete();
                }
            }
            的foreach(在范围Word.Range R)
            {
                r.InlineShapes.AddPicture(@C:\\ PathToNewImage \\ image.jpg的,裁判失踪,失踪参考,参考失踪);
            }
            WordApp.Quit(REF是的,裁判失踪,失踪参考);
        }
        #endregion
 }
}

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.

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);

解决方案

You can loop through the InlineShapes and replace the pictures

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
 }
}

这篇关于字自动化:使用C#替换图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆