【问题标题】:Find and replace text with an image in Word document在 Word 文档中查找并用图像替换文本
【发布时间】:2012-05-07 11:21:27
【问题描述】:

我已经弄清楚如何替换文档中的单词,因为使用 find 对象非常容易。现在,我正在努力用整个 word 文档中的实际徽标图像替换单词“logo”。

我想我可以遍历文档中的每个 Range 并说如果在该范围内找到单词 logo,我可以添加图片做:

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

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

有没有人有一个很好的解决方案?

【问题讨论】:

    标签: c# .net ms-word


    【解决方案1】:

    您可以通过以下方式做到这一点:此代码在准确的位置将文本替换为图像。

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

    【讨论】:

      【解决方案2】:

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

      查看可选的 Range 参数

      范围

      可选对象。将放置图片的位置 在文本中。如果范围没有折叠,图片将替换 范围;否则,插入图片。如果这个论点是 省略,图片自动放置。

      所以我会说你查询你拥有 logo 标签的当前位置,并将其用作 Range 值。

      【讨论】:

      • 谢谢。看起来在我的循环的情况下,范围实际上是文档中相当大的文本块 - 将图片添加到范围只是将它放在所有文本之前 - 我想我需要更好地指定范围不知何故?
      • 当您添加图片时,它会返回一个形状,请参阅此 link,并阅读“RelativeHorizo​​ntalPosition”和“RelativeVerticalPosition”属性的说明
      【解决方案3】:

      或者以这种方式:这会将文本替换为文档顶部的图像

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

      【讨论】:

        【解决方案4】:
        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);
            }
        

        【讨论】:

        • 虽然只允许代码回答,但请考虑添加一些描述。这将使答案更容易理解。
        猜你喜欢
        • 2013-05-03
        • 1970-01-01
        • 2017-08-06
        • 2021-10-13
        • 2019-05-20
        • 1970-01-01
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多