且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#字自动化:替换图片与文本

更新时间:2023-12-05 13:29:58

我是新来使用文字intelop所以也知道现在。该解决方案是非常简单和正在工作。只需添加使用以供将来参考。 BO.image是一个包含数据和数据类型的简单对象。

 私有静态无效FindAndReplaceImages(Word.Document研发,BO.ImageReplace图)
{
    反对失踪= System.Reflection.Missing.Value;
    名单< Word.Range>范围=新的名单,其中,Microsoft.Office.Interop.Word.Range>();
    的foreach(Word.InlineShape S在d.InlineShapes)
    {
        如果(s.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
        {
            ranges.Add(s.Range);
            s.Delete();
        }
    }

    的foreach(在范围Word.Range R)
    {
        如果(image.DataType ==图像)//那么的image.data是在磁盘上的位置
        {
            r.InlineShapes.AddPicture(的image.data,REF失踪,失踪参考,参考失踪);
        }
        否则,如果(image.DataType ==字)//那么的image.data是一个字符串
        {
            r.InsertBefore(的image.data);
        }
    }
}
 

I am programming a software and it requires images to be replaced with either images or text. I found some code to replace images with images it works fine. I want to tweak this code so that i can also replace images with text. I know there are better ways to do it but I specifically need it done using Interlope. Any help would be appreciated.

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

I am new to using word intelop so did now know. The solution was quite simple and is working. Just adding to use for future reference. BO.image is a simple object containing data and dataType.

private static void FindAndReplaceImages(Word.Document d, BO.ImageReplace image)
{
    object missing = System.Reflection.Missing.Value;
    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)
    {
        if (image.DataType == "image")//then image.Data is a location on disk
        {
            r.InlineShapes.AddPicture(image.Data, ref missing, ref missing, ref missing);
        }
        else if(image.DataType == "word")//then image.Data is a string
        {
            r.InsertBefore(image.Data);
        }
    }
}