50字范文,内容丰富有趣,生活中的好帮手!
50字范文 > C# 操作word 指定书签位置插入图片并设置图片大小和位置

C# 操作word 指定书签位置插入图片并设置图片大小和位置

时间:2022-10-24 08:20:09

相关推荐

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

{

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。