当我们生成pdf文档时,为了让文档更有条理性和可读性,我们通常在段落前面添加项目符号或项目编号,使整个文档层次更加分明。本文将介绍如何使用spire.pdf创建项目符号列表和多级项目编号列表。
创建项目符号列表
c#
//新建pdf文档并添加一页空pdf页面
pdfdocument doc = new pdfdocument();
pdfpagebase page = doc.pages.add(pdfpagesize.a4);
float y = 10;
//添加标题并设置文本和格式
pdfbrush brush1 = pdfbrushes.black;
pdftruetypefont font1 = new pdftruetypefont(new font("宋体", 18f, fontstyle.regular), true);
pdfstringformat format1 = new pdfstringformat(pdftextalignment.center);
page.canvas.drawstring("2017年度工作计划", font1, brush1, page.canvas.clientsize.width / 2, y, format1);
y = y font1.measurestring("2017年度工作计划", format1).height;
y = y 5;
//添加列表文字及设置文字格式
rectanglef rctg = new rectanglef(new pointf(0, 0), page.canvas.clientsize);
pdflineargradientbrush brush
= new pdflineargradientbrush(rctg, color.darkblue, color.brown, pdflineargradientmode.vertical);
pdftruetypefont font2 = new pdftruetypefont(new font("宋体", 15f, fontstyle.regular), true);
string formatted = "销售部工作计划\n人生就是博尊龙凯时的技术支持部工作计划\n开发部工作计划";
//设置项目符号列表及样式
pdflist list = new pdflist(formatted);
list.font = font2;
list.indent = 8;
list.textindent = 5;
list.brush = brush;
pdflayoutresult result = list.draw(page, 0, y);
y = result.bounds.bottom;
//保存文件
doc.savetofile("列表.pdf");
vb.net
'新建pdf文档并添加一页空pdf页面
dim doc as new pdfdocument()
dim page as pdfpagebase = doc.pages.add(pdfpagesize.a4)
dim y as single = 10
'添加标题并设置文本和格式
dim brush1 as pdfbrush = pdfbrushes.black
dim font1 as new pdftruetypefont(new font("宋体", 18f, fontstyle.regular), true)
dim format1 as new pdfstringformat(pdftextalignment.center)
page.canvas.drawstring("2017年度工作计划", font1, brush1, page.canvas.clientsize.width / 2, y, format1)
y = y font1.measurestring("2017年度工作计划", format1).height
y = y 5
'添加列表文字及设置文字格式
dim rctg as new rectanglef(new pointf(0, 0), page.canvas.clientsize)
dim brush as new pdflineargradientbrush(rctg, color.darkblue, color.brown, pdflineargradientmode.vertical)
dim font2 as new pdftruetypefont(new font("宋体", 15f, fontstyle.regular), true)
dim formatted as [string] = "销售部工作计划" & vblf & "人生就是博尊龙凯时的技术支持部工作计划" & vblf & "开发部工作计划"
'设置项目符号列表及样式
dim list as new pdflist(formatted)
list.font = font2
list.indent = 8
list.textindent = 5
list.brush = brush
dim result as pdflayoutresult = list.draw(page, 0, y)
y = result.bounds.bottom
'保存文件
doc.savetofile("列表.pdf")
多级编号列表
c#
//新建pdf文档并添加一页空pdf页面
pdfdocument doc = new pdfdocument();
pdfpagebase page = doc.pages.add(pdfpagesize.a4);
float y = 10;
//添加标题并设置文本和格式
pdfbrush brush1 = pdfbrushes.black;
pdftruetypefont font1 = new pdftruetypefont(new font("宋体", 18f, fontstyle.regular), true);
pdfstringformat format1 = new pdfstringformat(pdftextalignment.center);
page.canvas.drawstring("多级编号列表", font1, brush1, page.canvas.clientsize.width / 2, y, format1);
y = y font1.measurestring("多级编号列表", format1).height;
y = y 5;
//添加文本并设置字体
rectanglef rctg = new rectanglef(new pointf(0, 0), page.canvas.clientsize);
pdflineargradientbrush brush
= new pdflineargradientbrush(rctg, color.darkblue, color.brown, pdflineargradientmode.vertical);
pdftruetypefont font2 = new pdftruetypefont(new font("宋体", 15f, fontstyle.regular), true);
string formatted1 = "第一个一级标题";
string formatted2 = "第一个二级子标题";
string formatted3 = "第二个二级子标题";
//设置编号列表样式
pdforderedmarker marker1 = new pdforderedmarker(pdfnumberstyle.lowerroman, new pdffont(pdffontfamily.helvetica, 12f));
pdforderedmarker marker2 = new pdforderedmarker(pdfnumberstyle.numeric, new pdffont(pdffontfamily.helvetica, 10f));
//为第一级编号列表设置格式
pdfsortedlist list1 = new pdfsortedlist(font2);
list1.indent = 0;
list1.textindent = 5;
list1.brush = brush;
list1.marker = marker1;
//为第二级编号列表设置格式
pdfsortedlist list2 = new pdfsortedlist(font2);
list2.marker = marker2;
list2.brush = brush;
list2.textindent = 10;
//写入文本
pdflistitem item = list1.items.add(formatted1);
item.sublist = list2;
list2.items.add(formatted2);
list2.items.add(formatted3);
//设置pdf文档布局和位置
pdftextlayout textlayout = new pdftextlayout();
textlayout.break = pdflayoutbreaktype.fitpage;
textlayout.layout = pdflayouttype.paginate;
list1.draw(page, new pointf(0, y), textlayout);
//保存文件
doc.savetofile("多级列表.pdf");
vb.net
'新建pdf文档并添加一页空pdf页面
dim doc as new pdfdocument()
dim page as pdfpagebase = doc.pages.add(pdfpagesize.a4)
dim y as single = 10
'添加标题并设置文本和格式
dim brush1 as pdfbrush = pdfbrushes.black
dim font1 as new pdftruetypefont(new font("宋体", 18f, fontstyle.regular), true)
dim format1 as new pdfstringformat(pdftextalignment.center)
page.canvas.drawstring("多级编号列表", font1, brush1, page.canvas.clientsize.width / 2, y, format1)
y = y font1.measurestring("多级编号列表", format1).height
y = y 5
'添加文本并设置字体
dim rctg as new rectanglef(new pointf(0, 0), page.canvas.clientsize)
dim brush as new pdflineargradientbrush(rctg, color.darkblue, color.brown, pdflineargradientmode.vertical)
dim font2 as new pdftruetypefont(new font("宋体", 15f, fontstyle.regular), true)
dim formatted1 as [string] = "第一个一级标题"
dim formatted2 as [string] = "第一个二级子标题"
dim formatted3 as [string] = "第二个二级子标题"
'设置编号列表样式
dim marker1 as new pdforderedmarker(pdfnumberstyle.lowerroman, new pdffont(pdffontfamily.helvetica, 12f))
dim marker2 as new pdforderedmarker(pdfnumberstyle.numeric, new pdffont(pdffontfamily.helvetica, 10f))
'为第一级编号列表设置格式
dim list1 as new pdfsortedlist(font2)
list1.indent = 0
list1.textindent = 5
list1.brush = brush
list1.marker = marker1
'为第二级编号列表设置格式
dim list2 as new pdfsortedlist(font2)
list2.marker = marker2
list2.brush = brush
list2.textindent = 10
'写入文本
dim item as pdflistitem = list1.items.add(formatted1)
item.sublist = list2
list2.items.add(formatted2)
list2.items.add(formatted3)
'设置pdf文档布局和位置
dim textlayout as new pdftextlayout()
textlayout.break = pdflayoutbreaktype.fitpage
textlayout.layout = pdflayouttype.paginate
list1.draw(page, new pointf(0, y), textlayout)
'保存文件
doc.savetofile("多级列表.pdf")