0

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

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

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

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

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

4

4 回答 4

2

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

      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);
                }
            }
于 2017-03-27T14:37:17.717 回答
0

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

请参阅可选的 Range 参数

范围

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

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

于 2012-05-07T11:31:02.570 回答
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);
            }
        }
于 2017-03-27T14:41:42.697 回答
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);
    }
于 2020-05-24T19:19:33.847 回答