前面我们介绍了如何使用spire.doc,spire.doc也支持为word文档中特定的某段落或某文添加底纹背景,以达到美化文档和突出显示文档内容的效果。该文将详细介绍如何使用c#为word文档中指定的段落或文字添加底色。
c#
//创建document实例并加载示例文档
document document = new document();
document.loadfromfile("sample.docx");
//获取第一个段落并为其设置底色
paragraph paragaph = document.sections[0].paragraphs[0];
paragaph.format.backcolor = color.yellowgreen;
//为第三段里查找的特定文字添加底色
paragaph = document.sections[0].paragraphs[2];
textselection selection = paragaph.find("spire.doc for .net", false, true);
textrange range = selection.getasonerange();
range.characterformat.textbackgroundcolor = color.yellow;
//保存文档
document.savetofile("result.docx", fileformat.docx);
vb.net
'创建document实例并加载示例文档
dim document as document = new document
document.loadfromfile("sample.docx")
'获取第一个段落并为其设置底色
dim paragaph as paragraph = document.sections(0).paragraphs(0)
paragaph.format.backcolor = color.yellowgreen
'为第三段里查找的特定文字添加底色
paragaph = document.sections(0).paragraphs(2)
dim selection as textselection = paragaph.find("spire.doc for .net", false, true)
dim range as textrange = selection.getasonerange
range.characterformat.textbackgroundcolor = color.yellow
'保存文档
document.savetofile("result.docx", fileformat.docx)
效果图: