问题
I have figured out how to replace words in documents as this is fairly easy using the find object. Now I am struggling with replacing the word "logo" with an actual logo image throughout a word document.
I figured I can loop through each Range in the document and say that if the word logo is found in the range, I can add a picture doing:
foreach (Range rngStory in doc.StoryRanges)
{
rngStory.InlineShapes.AddPicture(filename);
}
The problem is that this will add the picture to the top of the range and not exactly where the text logo is.
Does anyone have a good solution for doing this?
回答1:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshapes.addpicture%28v=office.11%29.aspx
See the optional Range parameter
Range
Optional Object. The location where the picture will be placed in the text. If the range isn't collapsed, the picture replaces the range; otherwise, the picture is inserted. If this argument is omitted, the picture is placed automatically.
So I would say you query for the current position where you have you logo tag, and use that as the Range value.
回答2:
You can do this by following way: This Code replaces text to image at the accurate position.
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);
}
}
回答3:
Or in this way: This replaces text to image on the top of the document
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);
}
来源:https://stackoverflow.com/questions/10481188/find-and-replace-text-with-an-image-in-word-document