簡體   English   中英

如何在C#中查找文本並將其替換為圖像

[英]How to find a text and replace it with an image in C#

我有這段代碼,用於搜索文本並將其替換為MS Word文檔中的另一個文本。 我想做類似的操作。 查找文本,然后將其替換為圖像。 我可以傳遞文件位置,圖像位置。

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 replacet)
            {
                string filepath = filep;
                string Findtext = findt;
                string ReplaceText = replacet;
                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.Text = ReplaceText;
                //myFind.Replacement.
                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.Text = ReplaceText;
                        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.Text = ReplaceText;
                        myNextStoryFind.Wrap = word.WdFindWrap.wdFindContinue;
                        myNextStoryFind.Execute(Replace: word.WdReplace.wdReplaceAll);

                        nextStoryRange = nextStoryRange.NextStoryRange;
                    }

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

        }
    }

我認為您可以嘗試這樣的事情:

var doc = app.Documents.add(path.GetFullPath(@"Docs/yourDoc.docx")), visible: false);
doc.Activate();
String textToReplace = "your text";
var selected = app.Selection;
selected.Text = string.Format("[{0}]", keyword);
selected.Find.Execute(Replace: WdReplace.wdReplaceNone);
selected.Range.Select();

var imgPath = Path.GetFullPath(string.Format(yourImage))
selected.InLineShapes.AddPicture(FileName: imgPath, LinkToFile: false, SaveWithDocument: true);

doc.SaveAs(path.GetFullPath(@"Docs/yourDoc.docx"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM