在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)