在書簽位置插入文字
//加載模板文檔
Document document = new Document(Server.MapPath("~/File/評(píng)價(jià)結(jié)果.doc"));
//創(chuàng)建書簽導(dǎo)航器
BookmarksNavigator bn = new BookmarksNavigator(document);
//添加一個(gè)section到文檔
Section newSec = document.AddSection();
//目標(biāo)內(nèi)容
var name = "張三";
//添加段落到section
newSec.AddParagraph().AppendText(name);
//獲取段落內(nèi)容
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);
//移動(dòng)到書簽 “name”, 刪除它的內(nèi)容并保留格式
bn.MoveToBookmark("name", true, true);
bn.DeleteBookmarkContent(true);
//用新添加段落的內(nèi)容替換掉原書簽的內(nèi)容并保留格式
bn.ReplaceBookmarkContent(part, true, true);
在書簽位置插入圖片
//加載一個(gè)含有書簽的Word文檔
Document document = new Document(Server.MapPath("~/File/評(píng)價(jià)結(jié)果.doc"));
//創(chuàng)建BookmarksNavigator實(shí)例
BookmarksNavigator bn = new BookmarksNavigator(document);
//找到名為Spire的書簽
bn.MoveToBookmark("圖片", true, true);
//添加一個(gè)secton并命名為section0
Section section0 = document.AddSection();
//為section0添加一個(gè)段落
Paragraph paragraph = section0.AddParagraph();
//加載一張圖片
Image image = Image.FromFile("1.png");
//為段落添加圖片
DocPicture picture = paragraph.AppendPicture(image);
//把含有圖片的段落插入到書簽位置
bn.InsertParagraph(paragraph);
document.Sections.Remove(section0);
在書簽位置插入圖表
Document document = new Document(Server.MapPath("~/File/Model/周報(bào).doc"));
BookmarksNavigator bn = new BookmarksNavigator(document);
//添加一個(gè)節(jié)
var section0 = document.AddSection();
//在該節(jié)中添加一個(gè)段
var newPara = section0.AddParagraph();
//將指定大小的柱狀圖添加到段落中
var shape = newPara.AppendChart(ChartType.Column, 400, 252);
//創(chuàng)建圖表對(duì)象
Chart chart = shape.Chart;
//設(shè)置圖表標(biāo)題
chart.Title.Text = "柱狀圖";
ChartSeriesCollection seriesColl = chart.Series;
//清除圖表的默認(rèn)系列數(shù)據(jù)
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};
//添加一個(gè)具有指定系列名稱、類別名稱和系列值的自定義系列到圖表中
seriesColl.Add("李四", categories1, dataArr1);
seriesColl.Add("張三", categories2, dataArr2);
bn.MoveToBookmark("SWZZT",true,true);
bn.InsertParagraph(newPara);
document.Sections.Remove(section0);
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-737608.html
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-737608.html
到了這里,關(guān)于Spire.doc讀取模板文檔,并在書簽處插入內(nèi)容的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!