Word .NET库组件Spire.Doc系列教程(32): 在书签中位置插入图片和表格

本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,本篇文章介绍了如何在书签中位置插入图片和表格。

Spire.Doc for .NET是一个专业的Word .NET库,设计用于帮助开发人员高效地开发创建、阅读、编写、转换和打印任何来自.NET( C#, VB.NET, ASP.NET)平台的Word文档文件的功能。

本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,本篇文章介绍了如何在书签中位置插入图片和表格。>>下载Spire.Doc最新试用版体验


在 Word 文档的书签位置插入图片

Word文档的书签广泛用于指定一个特定的位置或为段落提供简短的信息。如果把图片添加到书签的位置,那么书签会变得显而易见。Spire.Doc 提供了一个BookmarksNavigator类用来找到书签,然后使用AppendPicture(Image image)方法来添加图片。下面是详细的步骤:

//加载一个含有书签的Word文档Document document = new Document();document.LoadFromFile("示例.docx");//创建BookmarksNavigator实例BookmarksNavigator bn = new BookmarksNavigator(document);//找到名为Spire的书签bn.MoveToBookmark("Spire", true, true);//添加一个secton并命名为section0Section section0 = document.AddSection();//为section0添加一个段落Paragraph paragraph = section0.AddParagraph();//加载一张图片Image image = Image.FromFile("示例.png");//为段落添加图片DocPicture picture = paragraph.AppendPicture(image);//把含有图片的段落插入到书签位置bn.InsertParagraph(paragraph);document.Sections.Remove(section0);//保存文档并打开string output = "sample3.docx";document.SaveToFile(output, FileFormat.Docx);System.Diagnostics.Process.Start(output);

基于开发人员的需求,Spire.Doc同时提供了以下属性来设置图片的位置。

picture.TextWrappingStylepicture.HorizontalAlignmentpicture.HorizontalOriginpicture.HorizontalPositionpicture.VerticalAlignmentpicture.VerticalOriginpicture.VerticalPosition

效果图:

Word .NET库组件Spire.Doc系列教程: 在书签中位置插入图片和表格

C# 在 Word 书签位置插入表格

Word 书签可以帮助程序员快速定位到文档指定地点,下面展示如何使用Spire.Doc获取某一个书签,并在书签现有内容的后面插入表格。

//加载模板文档Document doc = new Document();doc.LoadFromFile(@"C:UsersAdministratorDesktopemployee.docx");//创建Table对象Table table = new Table(doc,true);     //创建模拟数据DataTable dt = new DataTable();dt.Columns.Add("id", typeof(string));dt.Columns.Add("name", typeof(string));dt.Columns.Add("job", typeof(string));dt.Columns.Add("email", typeof(string));dt.Columns.Add("salary", typeof(string));dt.Rows.Add(new string[] { "员工编号", "姓名", "职位", "邮箱", "电话" });dt.Rows.Add(new string[] { "0241", "王浩然", "销售", "wang_hao_ran @outlook.com", "137****2211" });dt.Rows.Add(new string[] { "0242", "李向阳", "销售", "xiangyang @outlook.com", "159****5470" });dt.Rows.Add(new string[] { "0243", "闫佳强", "经理", "yjq1988 @gmail.com", "182****6541" });//将数据填充至表格table.ResetCells(dt.Rows.Count, dt.Columns.Count);for (int i = 0; i < dt.Rows.Count; i++){    for (int j = 0; j < dt.Columns.Count; j++)    {        table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString());    }}//获取指定书签位置BookmarksNavigator navigator = new BookmarksNavigator(doc);navigator.MoveToBookmark("employee_table");//将表格添加至TextBodyPartTextBodyPart part = navigator.GetBookmarkContent();     part.BodyItems.Add(table);//替换书签内容navigator.ReplaceBookmarkContent(part);//保存文件doc.SaveToFile("output.docx", FileFormat.Docx2013);

效果图:

Word .NET库组件Spire.Doc系列教程: 在书签中位置插入图片和表格

推荐阅读:【想要快速完成文档格式转换吗pire系列组件格式转换完整攻略来啦!】


*购买Spire.Doc正版授权的朋友可以点击“咨询在线客服”哦~~

Spire-850x100.png

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年8月6日
下一篇 2019年8月6日

相关推荐

发表回复

登录后才能评论