C#字自动化:替换图片与文本 [英] C# Word Automation : Replace picture with Text

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

问题描述

我编程软件,它需要的图像替换为图像或文本。 我发现了一些code与图片更换图片它工作正常。 我想调整这个code,这样我也可以用文字替换图片。 我知道有更好的方法来做到这一点,但我特别需要它做使用侵犯。 任何帮助将是AP preciated。

 使用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;
    私有对象是=真实;
    私有对象没有= FALSE;
    私人Word.Document D组;
    私人目标文件名= @C:\ FullPathToFile \ example.doc;
    #endregion

    #地区方法
    公共无效UpdateDoc()
    {
        D = WordApp.Documents.Open(REF文件名,参考失踪,裁判没有,参考失踪,
           裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,
           裁判失踪,失踪参考,裁判是的,裁判失踪,失踪参考,参考失踪,失踪参考);
        名单< 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)
        {
            r.InlineShapes.AddPicture(@C:\ PathToNewImage \ image.jpg文件,参考失踪,失踪参考,参考失踪);
        }
        WordApp.Quit(REF是的,裁判失踪,失踪参考);
    }
    #endregion
 }
}
 

解决方案

我是新来使用文字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);
        }
    }
}

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

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