使用Word文档中的图像查找和替换文本

时间:2012-05-07 11:21:28

标签: c# .net ms-word

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

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

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

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

有没有人有这样做的好方法?

4 个答案:

答案 0 :(得分: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);
                }
            }

答案 1 :(得分:0)

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

请参阅可选的Range参数

  

范围

     

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

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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