Spire.doc读取模板文档,并在书签处插入内容

在书签位置插入文字

csharp 复制代码
//加载模板文档 
Document document = new Document(Server.MapPath("~/File/评价结果.doc"));
//创建书签导航器
BookmarksNavigator bn = new BookmarksNavigator(document);
//添加一个section到文档 
Section newSec = document.AddSection();
//目标内容
var name = "张三";
//添加段落到section
newSec.AddParagraph().AppendText(name); 
//获取段落内容 
ParagraphBase firstReplacementParagraph = newSec.Paragraphs[0].Items.FirstItem as ParagraphBase;
ParagraphBase lastReplacementParagraph = newSec.Paragraphs[newSec.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
TextBodySelection selection = new TextBodySelection(firstReplacementParagraph, lastReplacementParagraph);
TextBodyPart part = new TextBodyPart(selection);
//移动到书签 "name", 删除它的内容并保留格式  
bn.MoveToBookmark("name", true, true);
bn.DeleteBookmarkContent(true);
//用新添加段落的内容替换掉原书签的内容并保留格式 
bn.ReplaceBookmarkContent(part, true, true);

在书签位置插入图片

csharp 复制代码
//加载一个含有书签的Word文档
Document document = new Document(Server.MapPath("~/File/评价结果.doc"));
//创建BookmarksNavigator实例
BookmarksNavigator bn = new BookmarksNavigator(document);
//找到名为Spire的书签
bn.MoveToBookmark("图片", true, true);
//添加一个secton并命名为section0
Section section0 = document.AddSection();
//为section0添加一个段落
Paragraph paragraph = section0.AddParagraph();
//加载一张图片
Image image = Image.FromFile("1.png");
//为段落添加图片
DocPicture picture = paragraph.AppendPicture(image);
//把含有图片的段落插入到书签位置
bn.InsertParagraph(paragraph);
document.Sections.Remove(section0);

在书签位置插入图表

csharp 复制代码
Document document = new Document(Server.MapPath("~/File/Model/地下水动态监测系统简报-周报.doc"));
BookmarksNavigator bn = new BookmarksNavigator(document);
//添加一个节
var section0 = document.AddSection();
//在该节中添加一个段
var newPara = section0.AddParagraph();
//将指定大小的柱状图添加到段落中
var shape = newPara.AppendChart(ChartType.Column, 400, 252);
//创建图表对象
Chart chart = shape.Chart;
//设置图表标题
chart.Title.Text = "柱状图";
ChartSeriesCollection seriesColl = chart.Series;
//清除图表的默认系列数据
seriesColl.Clear();
string[] categories1 = new string[]{"甲","乙","丙","丁"};
double[] dataArr1 = new double[]{1.1,2.2,3.3,4.4};
string[] categories2 = new string[]{"甲","乙","丙","丁"};
double[] dataArr2 = new double[]{5.5,6.6,7.7,8.8};
//添加一个具有指定系列名称、类别名称和系列值的自定义系列到图表中
seriesColl.Add("李四", categories1, dataArr1);
seriesColl.Add("张三", categories2, dataArr2);
bn.MoveToBookmark("SWZZT",true,true);
bn.InsertParagraph(newPara);
document.Sections.Remove(section0);
相关推荐
future_studio1 小时前
聊聊 Unity(小白专享、C# 小程序 之 图片播放器)
unity·小程序·c#
c#上位机7 小时前
wpf中Grid的MouseDown 事件无法触发的原因
c#·wpf
CodeCraft Studio8 小时前
国产化PDF处理控件Spire.PDF教程:如何在 C# 中从 HTML 和 PDF 模板生成 PDF
pdf·c#·html·.net·spire.pdf·pdf文档开发·html创建模板pdf
ysdysyn9 小时前
.NET 10深度解析:性能革新与开发生态的全新篇章
c#·.net
L X..12 小时前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎
reasonsummer14 小时前
【办公类-115-06】20250920职称资料上传04——docx复制、docx转PDF(课程表11个)
开发语言·windows·python·c#
fs哆哆1 天前
在VB.NET中,有没有 ?.这个运算符
java·开发语言·.net
荔园微风1 天前
微软ML.NET技术详解:从数据科学到生产部署的全栈解决方案
microsoft·.net
William_cl1 天前
一、前置基础(MVC学习前提)_核心特性_【C# 泛型入门】为什么说 List<T>是程序员的 “万能收纳盒“?避坑指南在此
学习·c#·mvc
c#上位机1 天前
wpf之命令
c#·wpf