在word文档中,如果有多条并列关系的信息(段落)。为了更清晰,我们可以在前面添加项目符号或项目编号,使更具有条理性,使层次更分明。本文将介绍如何使用spire.doc创建项目符号列表和多级项目编号列表。
创建项目符号列表
c#
//新建word文档
document doc = new document();
section section = doc.addsection();
//添加七个段落并分别添加文字
paragraph para1 = section.addparagraph();
para1.appendtext("人力资源管理制度");
paragraph para2 = section.addparagraph();
para2.appendtext("新员工管理制度");
paragraph para3 = section.addparagraph();
para3.appendtext("出勤管理制度");
paragraph para4 = section.addparagraph();
para4.appendtext("考核管理制度");
paragraph para5 = section.addparagraph();
para5.appendtext("培训管理制度");
paragraph para6 = section.addparagraph();
para6.appendtext("内训师管理制度");
paragraph para7 = section.addparagraph();
para7.appendtext("薪酬管理制度");
//创建段落格式(字体)
paragraphstyle style = new paragraphstyle(doc);
style.name = "fontstyle";
style.characterformat.fontname = "宋体";
style.characterformat.fontsize = 12f;
doc.styles.add(style);
for (int i = 0; i < section.paragraphs.count; i )
{
//从第二段开始应用项目符号排列
if (i != 0)
{
section.paragraphs[i].applystyle(builtinstyle.listbullet2);
}
//应用字体格式到每一段
section.paragraphs[i].applystyle("fontstyle");
}
//保存文档
doc.savetofile("项目列表.docx", fileformat.docx2013);
vb.net
'新建word文档
dim doc as document = new document
dim section as section = doc.addsection
'添加七个段落并分别添加文字
dim para1 as paragraph = section.addparagraph
para1.appendtext("人力资源管理制度")
dim para2 as paragraph = section.addparagraph
para2.appendtext("新员工管理制度")
dim para3 as paragraph = section.addparagraph
para3.appendtext("出勤管理制度")
dim para4 as paragraph = section.addparagraph
para4.appendtext("考核管理制度")
dim para5 as paragraph = section.addparagraph
para5.appendtext("培训管理制度")
dim para6 as paragraph = section.addparagraph
para6.appendtext("内训师管理制度")
dim para7 as paragraph = section.addparagraph
para7.appendtext("薪酬管理制度")
'创建段落格式(字体)
dim style as paragraphstyle = new paragraphstyle(doc)
style.name = "fontstyle"
style.characterformat.fontname = "宋体"
style.characterformat.fontsize = 12!
doc.styles.add(style)
dim i as integer = 0
do while (i < section.paragraphs.count)
'从第二段开始应用项目符号排列
if (i <> 0) then
section.paragraphs(i).applystyle(builtinstyle.listbullet2)
end if
'应用字体格式到每一段
section.paragraphs(i).applystyle("fontstyle")
i = (i 1)
loop
'保存文档
doc.savetofile("项目列表.docx", fileformat.docx2013)
创建多级编号列表
c#
//新建word文档
document doc = new document();
section section = doc.addsection();
//初始化liststyle对象,指定list类型为数字列表
liststyle liststyle = new liststyle(doc, listtype.numbered);
//指定名字
liststyle.name = "levelstyle";
//设定一级列表模式为阿拉伯数字
liststyle.levels[0].patterntype = listpatterntype.arabic;
//设置二级列表数字前缀及模式
liststyle.levels[1].numberprefix = "\x0000.";
liststyle.levels[1].patterntype = listpatterntype.arabic;
//设置三级列表数字前缀及模式
liststyle.levels[2].numberprefix = "\x0000.\x0001.";
liststyle.levels[2].patterntype = listpatterntype.arabic;
//在liststyles集合中添加新建的list style
doc.liststyles.add(liststyle);
//创建字体格式
spire.doc.formatting.characterformat format = new spire.doc.formatting.characterformat(doc);
format.fontname = "宋体";
//添加段落,设置一级序列
paragraph paragraph = section.addparagraph();
textrange tr= paragraph.appendtext("第一个一级标题");
tr.applycharacterformat(format); //应用字体格式
paragraph.applystyle(builtinstyle.heading1); //应用标题1样式
paragraph.listformat.applystyle("levelstyle"); //应用列表样式
//添加段落,设置一级序列
paragraph = section.addparagraph();
tr = paragraph.appendtext("第二个一级标题");
tr.applycharacterformat(format);
paragraph.applystyle(builtinstyle.heading1);
paragraph.listformat.applystyle("levelstyle");
//添加段落,设置二级序列
paragraph = section.addparagraph();
tr = paragraph.appendtext("第一个二级子标题");
tr.applycharacterformat(format);
paragraph.applystyle(builtinstyle.heading2);
paragraph.listformat.listlevelnumber = 1; //设置等级为第二等级
paragraph.listformat.applystyle("levelstyle");
//添加段落,设置二级序列
paragraph = section.addparagraph();
tr = paragraph.appendtext("第二个二级子标题");
tr.applycharacterformat(format);
paragraph.applystyle(builtinstyle.heading2);
paragraph.listformat.continuelistnumbering();
paragraph.listformat.applystyle("levelstyle");
//添加段落,设置三级序列
paragraph = section.addparagraph();
tr = paragraph.appendtext("三级子标题");
tr.applycharacterformat(format);
paragraph.applystyle(builtinstyle.heading5);
paragraph.listformat.listlevelnumber = 2; //设置等级为第三等级
paragraph.listformat.applystyle("levelstyle");
//添加段落,设置一级序列
paragraph = section.addparagraph();
tr = paragraph.appendtext("第三个一级标题");
tr.applycharacterformat(format);
paragraph.applystyle(builtinstyle.heading1);
paragraph.listformat.applystyle("levelstyle");
//保存文档
doc.savetofile("多级列表.docx", fileformat.docx);
vb.net
'新建word文档
dim doc as document = new document
dim section as section = doc.addsection
'初始化liststyle对象,指定list类型为数字列表
dim liststyle as liststyle = new liststyle(doc, listtype.numbered)
'指定名字
dim liststyle.name as w = "levelstyle"
'设定一级列表模式为阿拉伯数字
liststyle.levels(0).patterntype = listpatterntype.arabic
'设置二级列表数字前缀及模式
liststyle.levels(1).numberprefix = "\x0000."
liststyle.levels(1).patterntype = listpatterntype.arabic
'设置三级列表数字前缀及模式
liststyle.levels(2).numberprefix = "\x0000.\x0001."
liststyle.levels(2).patterntype = listpatterntype.arabic
'在liststyles集合中添加新建的list style
doc.liststyles.add(liststyle)
'创建字体格式
dim format as spire.doc.formatting.characterformat = new spire.doc.formatting.characterformat(doc)
format.fontname = "宋体"
'添加段落,设置一级序列
dim paragraph as paragraph = section.addparagraph
dim tr as textrange = paragraph.appendtext("第一个一级标题")
tr.applycharacterformat(format)'应用字体格式
paragraph.applystyle(builtinstyle.heading1)'应用标题1样式
paragraph.listformat.applystyle("levelstyle")'应用列表样式
'添加段落,设置一级序列
paragraph = section.addparagraph
tr = paragraph.appendtext("第二个一级标题")
tr.applycharacterformat(format)
paragraph.applystyle(builtinstyle.heading1)
paragraph.listformat.applystyle("levelstyle")
'添加段落,设置二级序列
paragraph = section.addparagraph
tr = paragraph.appendtext("第一个二级子标题")
tr.applycharacterformat(format)
paragraph.applystyle(builtinstyle.heading2)
paragraph.listformat.listlevelnumber = 1'设置等级为第二等级
paragraph.listformat.applystyle("levelstyle")
'添加段落,设置二级序列
paragraph = section.addparagraph
tr = paragraph.appendtext("第二个二级子标题")
tr.applycharacterformat(format)
paragraph.applystyle(builtinstyle.heading2)
paragraph.listformat.continuelistnumbering
paragraph.listformat.applystyle("levelstyle")
'添加段落,设置三级序列
paragraph = section.addparagraph
tr = paragraph.appendtext("三级子标题")
tr.applycharacterformat(format)
paragraph.applystyle(builtinstyle.heading5)
paragraph.listformat.listlevelnumber = 2'设置等级为第三等级
paragraph.listformat.applystyle("levelstyle")
'添加段落,设置一级序列
paragraph = section.addparagraph
tr = paragraph.appendtext("第三个一级标题")
tr.applycharacterformat(format)
paragraph.applystyle(builtinstyle.heading1)
paragraph.listformat.applystyle("levelstyle")
'保存文档
doc.savetofile("多级列表.docx", fileformat.docx)