900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > c# 在word文件指定位置 插入图片

c# 在word文件指定位置 插入图片

时间:2019-10-13 08:49:16

相关推荐

c# 在word文件指定位置 插入图片

使用 替换 word 文件中 “书签”方法

一、 在要插入图片的位置加上“书签”

二、 c# 程序添加 office 引用

在 com 中添加下面两个引用

三、编写代码

object Nothing = System.Reflection.Missing.Value;Microsoft.Office.Interop.Word.Application worldApp = new Microsoft.Office.Interop.Word.Application();object filename = @"E:\调查表.docx";//定义该插入图片是否为外部链接object linkToFile = true;//定义插入图片是否随word文档一起保存object saveWithDocument = true;//打开word文档Microsoft.Office.Interop.Word.Document doc = worldApp.Documents.Open(ref strDocx, 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);//标签object bookMark = "cw_w";//图片string replacePic =@"E:\img.jpg";if (doc.Bookmarks.Exists(Convert.ToString(bookMark)) == true){//查找书签doc.Bookmarks.get_Item(ref bookMark).Select();//设置图片位置worldApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//在书签的位置添加图片Microsoft.Office.Interop.Word.InlineShape inlineShape = worldApp.Selection.InlineShapes.AddPicture(checkedPath, ref linkToFile, ref saveWithDocument, ref Nothing);//设置图片大小inlineShape.Width = 12;inlineShape.Height = 12;doc.SaveAs2(saveDocx);doc.Close(ref Nothing, ref Nothing, ref Nothing);}else{//word文档中不存在该书签,关闭文档doc.Close(ref Nothing, ref Nothing, ref Nothing);}

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