c# - 在 C# 中用同一位置的图像替换文本

标签 c# ms-word office-interop

此代码用图像替换文本,但它放置图像的多个副本并将它们放在文档的开头。我希望将图像放置在与文本相同的位置。我的查找文本在表格单元格中可用。是因为这个吗?

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.IO;
    using word = Microsoft.Office.Interop.Word;
    using System.Runtime.InteropServices;
    //using System.Drawing;


    namespace WritingIntoDocx
    {
        [ComVisible(true)]
        public interface IMyClass
        {

          void DocumentDigitalSign(string filep,string findt,string replacet);
        }


        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]

       public class Program : IMyClass
        {
             public void DocumentDigitalSign(string filep, string findt, string imagepath)
            {
                string filepath = filep;
                string Findtext = findt;
                word.Application app = new word.Application();
                word.Document doc = app.Documents.Open(filepath);

                word.Range myStoryRange = doc.Range();

                //First search the main document using the Selection
                word.Find myFind = myStoryRange.Find;
                myFind.Text = Findtext;               myFind.Replacement.Application.Selection.InlineShapes.AddPicture(imagepath);
                myFind.Forward = true;
                myFind.Wrap = word.WdFindWrap.wdFindContinue;
                myFind.Format = false;
                myFind.MatchCase = false;
                myFind.MatchWholeWord = false;
                myFind.MatchWildcards = false;
                myFind.MatchSoundsLike = false;
                myFind.MatchAllWordForms = false;
                myFind.Execute(Replace: word.WdReplace.wdReplaceAll);

                //'Now search all other stories using Ranges
                foreach (word.Range otherStoryRange in doc.StoryRanges)
                {
                    if (otherStoryRange.StoryType != word.WdStoryType.wdMainTextStory)
                    {
                        word.Find myOtherFind = otherStoryRange.Find;
                        myOtherFind.Text = Findtext;          myOtherFind.Replacement.Application.Selection.InlineShapes.AddPicture(imagepath);
                        myOtherFind.Wrap = word.WdFindWrap.wdFindContinue;
                        myOtherFind.Execute(Replace: word.WdReplace.wdReplaceAll);
                    }

                    // 'Now search all next stories of other stories (doc.storyRanges dont seem to cascades in sub story)
                    word.Range nextStoryRange = otherStoryRange.NextStoryRange;
                    while (nextStoryRange != null)
                    {
                        word.Find myNextStoryFind = nextStoryRange.Find;
                        myNextStoryFind.Text = Findtext;
                        myNextStoryFind.Replacement.Application.Selection.InlineShapes.AddPicture(imagepath);
                        myNextStoryFind.Wrap = word.WdFindWrap.wdFindContinue;
                        myNextStoryFind.Execute(Replace: word.WdReplace.wdReplaceAll);

                        nextStoryRange = nextStoryRange.NextStoryRange;
                    }

                }
                app.Documents.Save();
                app.Documents.Close();
            }

        }


 }

最佳答案

Replacement.Application 是对应用程序对象的引用。当您对其调用 AddPicture() 时,图片会在执行查找操作之前立即插入到当前位置。

我看到了两种可能性:

  1. 加载图片,将其放入 Windows 剪贴板,然后执行查找操作,指定“^c”作为替换文本。 Word 会将“^c”替换为剪贴板的当前内容。这就是documentation说:

ReplaceWith
Type: System.Object
Optional Object.
The replacement text. To delete the text specified by the Find argument, use an empty string (""). You specify special characters and advanced search criteria just as you do for the Find argument. To specify a graphic object or other non-text item as the replacement, move the item to the Clipboard and specify "^c" for ReplaceWith.

  1. 不要使用 wdReplaceAll,而是使用 wdReplaceNone,这样查找操作本身不做任何替换。但是您随后有机会在找到的位置插入您的内容。循环执行此操作,直到不再出现为止。

关于c# - 在 C# 中用同一位置的图像替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36910946/

相关文章:

java - 如何在java中使用DOCX4J将图片放在Word(.docx)文件的某个地方

c# - 将 .docx 转换为 html

java - 如何在不安装 MS Office 且不使用 Interop Library 的情况下读取服务器中的 MS Office 文件?

c# - 在资源字典中的同一程序集中引用 WPF 控件类

c# - Windows Azure 网站中的自定义性能计数器

c# - 如何使用 FromBase64String 创建长度为 16 的 byte[]

java - 如何在java中获取microsoft word文档的页数?

c# - 如何以编程方式创建 Excel "sort/Filter combobox"?

ms-office - 在 Windows 服务器上安装 Microsoft Office?

c# - 如何在C#程序中执行存储过程