.net - How to find a text and replace it with an image in C# -


i have code searches text , replace text in ms word document. want similar kind of operation. find text , replace image. can pass file location, image location.

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 main document using 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 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 next stories of other stories (doc.storyranges dont seem 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();             }          }     } 

i think can try this:

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

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -