C# 操作word 指定书签位置插入图片并设置图片大小和位置
object Nothing = System.Reflection.Missing.Value;
//创建一个名为wordApp的组件对象
Application wordApp = new Application();
//word文档位置
object filename = @"E:\ceshi.doc";
//定义该插入图片是否为外部链接
object linkToFile = true;
//定义插入图片是否随word文档一起保存
object saveWithDocument = true;
//打开word文档
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing);
try
{
//标签
object bookMark = "para04";
//图片
string replacePic = @"E:\1.gif";
if (doc.Bookmarks.Exists(Convert.ToString(bookMark)) == true)
{
//查找书签
doc.Bookmarks.get_Item(ref bookMark).Select();
//设置图片位置
wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
//在书签的位置添加图片
InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture(replacePic, ref linkToFile, ref saveWithDocument, ref Nothing);
//设置图片大小
inlineShape.Width = 20;
inlineShape.Height = 20;
doc.Save();
}
else
{
//word文档中不存在该书签,关闭文档
doc.Close(ref Nothing,ref Nothing,ref Nothing);
}
}
catch
{
}