在 Word 文档中查找文本并将其替换为图像

本文关键字:替换 图像 文本 Word 文档 查找 | 更新日期: 2023-09-27 18:33:34

我已经想出了如何替换文档中的单词,因为使用查找对象这相当容易。现在,我正在努力在整个word文档中用实际的徽标图像替换"徽标"一词。

我想我可以遍历文档中的每个范围,并说如果在该范围中找到单词徽标,我可以添加一张图片:

foreach (Range rngStory in doc.StoryRanges)
{                
    rngStory.InlineShapes.AddPicture(filename);
}

问题是这会将图片添加到范围的顶部,而不是文本徽标的确切位置。

没有人有很好的解决方案来做到这一点?

在 Word 文档中查找文本并将其替换为图像

您可以通过以下方式执行此操作: 此代码将文本替换为准确位置的图像。

      Word.Application wordApp = new Word.Application();
      Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref outputFile, ref oMissing,
                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                     ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                doc.Activate();
              try
                {
                    //----------------------Replace--------------------------------
                    Microsoft.Office.Interop.Word.Find fnd = wordApp.ActiveWindow.Selection.Find;
                    fnd.ClearFormatting();
                    fnd.Replacement.ClearFormatting();
                    fnd.Forward = true;
                    fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    string imagePath = Server.MapPath("~/data/uploads/violations/resize/Image.jpg");
    var keyword = "LOGO";
                        var sel = wordApp.Selection;
                        sel.Find.Text = string.Format("[{0}]", keyword);
    wordApp.Selection.Find.Execute(keyword);
                        Word.Range range = wordApp.Selection.Range;
     if (range.Text.Contains(keyword))
                            {
                                //gets desired range here it gets last character to make superscript in range 
                                Word.Range temprange = doc.Range(range.End - 4, range.End);//keyword is of 4 charecter range.End - 4
                                temprange.Select();
                                Word.Selection currentSelection = wordApp.Selection;
                                //currentSelection.Font.Superscript = 1;
                                sel.Find.Execute(Replace: WdReplace.wdReplaceOne);
                                sel.Range.Select();
                                var imagePath1 = Path.GetFullPath(string.Format(imagePath, keyword));
                                sel.InlineShapes.AddPicture(FileName: imagePath1, LinkToFile: false, SaveWithDocument: true);
     }
}
catch { }
            finally
            {
                if (doc != null)
                {
                    ((_Document)doc).Close(ref oMissing, ref oMissing, ref oMissing);
                    Marshal.FinalReleaseComObject(doc);
                }
                if (wordApp != null)
                {
                    ((_Application)wordApp).Quit();
                    Marshal.FinalReleaseComObject(wordApp);
                }
            }

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshapes.addpicture%28v=office.11%29.aspx

请参阅可选的范围参数

范围

可选对象。将放置图片的位置在文中。如果范围未折叠,则图片将替换范围;否则,将插入图片。如果此参数是省略,图片将自动放置。

因此,我会说您查询具有徽标标签的当前位置,并将其用作范围值。

或者这样: 这会将文本替换为文档顶部的图像

  Word.Application wordApp = new Word.Application();
  Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref outputFile, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            doc.Activate();
          try
            {
                //----------------------Replace--------------------------------
                Microsoft.Office.Interop.Word.Find fnd = wordApp.ActiveWindow.Selection.Find;
                fnd.ClearFormatting();
                fnd.Replacement.ClearFormatting();
                fnd.Forward = true;
                fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
string imagePath = Server.MapPath("~/data/uploads/violations/resize/Image.jpg");
var keyword = "LOGO";
                    var sel = wordApp.Selection;
                    sel.Find.Text = string.Format("[{0}]", keyword);
wordApp.Selection.Find.Execute(keyword);
                    Word.Range range = wordApp.Selection.Range;

                            sel.Find.Execute(Replace: WdReplace.wdReplaceOne);
                            sel.Range.Select();
                            var imagePath1 = Path.GetFullPath(string.Format(imagePath, keyword));
                            sel.InlineShapes.AddPicture(FileName: imagePath1, LinkToFile: false, SaveWithDocument: true);


  }
  catch (Exception)
        {
        }
        finally
        {
            if (doc != null)
            {
                ((_Document)doc).Close(ref oMissing, ref oMissing, ref oMissing);
                Marshal.FinalReleaseComObject(doc);
            }
            if (wordApp != null)
            {
                ((_Application)wordApp).Quit();
                Marshal.FinalReleaseComObject(wordApp);
            }
        }
var wApp = new Application();
var wDoc = wApp.Documents.Open(Path.GetFullPath(filePath), ReadOnly: false);
imagePath = Path.GetFullPath(imagePath);
foreach (Range rng in wDoc.StoryRanges)
    while (rng.Find.Execute(keyword, Forward: true, Wrap: WdFindWrap.wdFindContinue))
    {
        rng.Select();
        wApp.Selection.InlineShapes.AddPicture(imagePath, false, true);
    }