云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > C# 插入、删除Word书签

C# 插入、删除Word书签

  • 62 次阅读
  • 3 次下载
  • 2025/6/22 23:25:56

【示例3】插入表格到书签所在段落

C#

using Spire.Doc;

using Spire.Doc.Documents; using System.Data;

namespace InsertTableToBookmark_Doc {

class Program {

static void Main(string[] args) {

//创建文档,加载文件

Document doc = new Document(); doc.LoadFromFile(\);

//创建Table对象

Table table = new Table(doc, true);

//创建模拟数据

DataTable dt = new DataTable(); dt.Columns.Add(\, typeof(string)); dt.Columns.Add(\, typeof(string)); dt.Columns.Add(\, typeof(string));

dt.Columns.Add(\, typeof(string)); dt.Rows.Add(new string[] { \,\, \, \ });

dt.Rows.Add(new string[] { \, \, \, \ }); dt.Rows.Add(new string[] { \, \, \, \ }); dt.Rows.Add(new string[] { \, \, \, \ });

//将数据填充至表格

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(\);

//将表格添加至TextBodyPart

TextBodyPart part = navigator.GetBookmarkContent(); part.BodyItems.Add(table);

//替换书签内容

navigator.ReplaceBookmarkContent(part);

//保存并打开文件

doc.SaveToFile(\, FileFormat.Docx2013);

System.Diagnostics.Process.Start(\); } } }

表格插入效果:

【示例4】删除书签

C#

using Spire.Doc;

using Spire.Doc.Documents;

namespace Removing {

class Program {

static void Main(string[] args) {

//实例化Document类,加载文档 Document doc = new Document(); doc.LoadFromFile(\);

//创建BookmarksNavigator实例

BookmarksNavigator navigator = new BookmarksNavigator(doc);

//指向特定的书签

navigator.MoveToBookmark(\);

//删除书签中的内容

navigator.DeleteBookmarkContent(false);

//删除书签

doc.Bookmarks.Remove(doc.Bookmarks.FindByName(\));

//保存并打开文档

doc.SaveToFile(\, FileFormat.Docx); System.Diagnostics.Process.Start(\); } } }

测试结果: 测试文档如下

删除书签后:

以上内容为本次“C#操作Word书签的方法”介绍如需转载,请注明出处。

搜索更多关于: C# 插入、删除Word书签 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

【示例3】插入表格到书签所在段落 C# using Spire.Doc; using Spire.Doc.Documents; using System.Data; namespace InsertTableToBookmark_Doc { class Program { static void Main(string[] args) { //创建文档,加载文件 Document doc = new Document(); doc.LoadFromFile(\); //创建Table

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com