列表是有序排列的项目集合,可用于在页面上突出展示重要信息、流程步骤,也可用于向读者展示文档内容概览。本文将展示如何使用 Spire.PDF for .NET 以编程的方式在 PDF 文档中创建有序或无序列表。
Spire.PDF for .NET 提供 PdfSortedList 类和 PdfList 类,分别代表有序列表和无序列表。而设置列表的内容、缩进、字体、标记样式及其他属性则需要使用 PdfSortedList 类和 PdfList 类下的属性或方法。下表列出了本文将用到的部分核心项目。
类、方法或属性 | 描述 |
PdfSortedList 类 | 代表 PDF 文档中的有序列表。 |
PdfList 类 | 代表 PDF 文档中的无序列表。 |
Brush 属性 | 获取或设置列表的绘制参数。 |
Font 属性 | 获取或设置列表的字体。 |
Indent 属性 | 获取或设置列表的缩进参数。 |
TextIndent 属性 | 获取或设置标记到项目间的缩进参数。 |
Items 属性 | 获取列表的项目。 |
Marker 属性 | 获取或设置列表标记的参数。 |
Draw() 方法 | 在页面指定位置绘制列表。 |
PdfOrderedMarker 类 | 代表有序列表的标记样式,如阿拉伯数字、字母、罗马数字。 |
PdfMarker 类 | 代表无序列表的项目符号样式。 |
安装 Spire.PDF for .NET
首先,我们需要将 Spire.PDF for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。可以从此链接下载 DLL 文件,也可以通过 NuGet 安装 DLL 文件。
PM> Install-Package Spire.PDF
在 PDF 文档中创建有序列表
有序列表是包含有一系列项目的容器,列表中的每个项目都以阿拉伯数字数字、字母或罗马数字进行标记。以下是使用 Spire.PDF for .NET 创建有序列表的详细操作步骤。
- 创建 PdfDocument 类的对象。
- 使用 PdfDocument.Pages.Add() 方法在文档中添加一个页面。
- 为列表创建 PdfBrush 类的对象和 PdfFont 类的对象。
- 创建 PdfOrderedMarker 类的对象并指定标记样式。
- 用字符串指定列表内容并基于字符串创建 PdfSortedList 类的对象。
- 通过 PdfSortedList 类下的属性设置字体、缩进、绘制和标记等参数。
- 使用 PdfSortedList.Draw() 方法将列表绘制在页面的指定位置。
- 使用 PdfDocument.SaveToFile() 方法保存 PDF 文档。
- C#
- VB.NET
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;
namespace ChangePDFPageSize
{
internal class Program
{
static void Main(string[] args)
{
//创建 PdfDocument 类的对象
PdfDocument doc = new PdfDocument();
//设置页边距
PdfMargins margins = new PdfMargins(30);
//添加一个页面
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margins);
//创建 PdfBrush 类的对象
PdfBrush brush = PdfBrushes.Black;
//创建字体
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("宋体", 11f), true);
PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);
//为列表创建 PdfOrderedMarker 类的对象
PdfOrderedMarker marker = new PdfOrderedMarker(PdfNumberStyle.LowerLatin, listFont);
//设置初始坐标
float x = 10;
float y = 20;
//绘制标题
String title = "需要满足的网页开发技能:";
page.Canvas.DrawString(title, titleFont, brush, x, y);
y = y + (float)titleFont.MeasureString(title).Height;
y = y + 5;
//创建有序列表
String listContent = "Command-line Unix\n"
+ "Vim\n"
+ "HTML\n"
+ "CSS\n"
+ "Python\n"
+ "JavaScript\n"
+ "SQL";
PdfSortedList list = new PdfSortedList(listContent);
//设置字体、段落缩进、文本缩进、绘制等参数
list.Font = listFont;
list.Indent = 2;
list.TextIndent = 4;
list.Brush = brush;
list.Marker = marker;
//将列表绘制在页面指定位置
list.Draw(page, x, y);
//保存文档
doc.SaveToFile("有序列表.pdf");
}
}
}
Imports System
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Lists
Namespace ChangePDFPageSize
Friend Class Program
Shared Sub Main(ByVal args() As String)
'创建 PdfDocument 类的对象 Dim doc As PdfDocument = New PdfDocument() '设置页边距
Dim margins As PdfMargins = New PdfMargins(30)
'添加一个页面 Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4,margins) '创建 PdfBrush 类的对象
Dim brush As PdfBrush = PdfBrushes.Black
'创建字体 Dim titleFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("宋体",11.0F),True) Dim listFont As PdfFont = New PdfFont(PdfFontFamily.TimesRoman,12.0F,PdfFontStyle.Regular) '为列表创建 PdfOrderedMarker 类的对象
Dim marker As PdfOrderedMarker = New PdfOrderedMarker(PdfNumberStyle.LowerLatin, listFont)
'设置初始坐标 Dim x As Single = 10 Dim y As Single = 20 '绘制标题
Dim title As String = "需要满足的网页开发技能:"
page.Canvas.DrawString(title, titleFont, brush, x, y)
y = y + CType(titleFont.MeasureString(title).Height, Single)
y = y + 5
'创建有序列表 Dim listContent As String = "Command-line Unix\n" + "Vim\n" + "HTML\n" + "CSS\n" + "Python\n" + "JavaScript\n" + "SQL" Dim list As PdfSortedList = New PdfSortedList(listContent) '设置字体、段落缩进、文本缩进、绘制等参数
list.Font = listFont
list.Indent = 2
list.TextIndent = 4
list.Brush = brush
list.Marker = marker
'将列表绘制在页面指定位置 list.Draw(page,x,y) '保存文档
doc.SaveToFile("有序列表.pdf")
End Sub
End Class
End Namespace
在 PDF 文档中创建多级编号列表
多级编号列表拥有至少一个二级列表,用来展示多级结构的数据。以下是使用 Spire.PDF for .NET 在 PDF 中创建多级编号列表的操作步骤。
- 创建 PdfDocument 类的对象。
- 使用 PdfDocument.Pages.Add() 方法在文档中添加一个页面。
- 创建 PdfOrderedMarker 类的对象,指定标记样式和编号。
- 用字符串指定列表内容并基于字符串创建一级列表。然后通过 PdfSortedList 类下的属性设置列表的字体、缩进、绘制和标记等参数。
- 重复以上步骤,创建二级、三级列表。
- 使用 PdfSortedList.Items[] 属性获取一级列表的指定项目,再用 PdfListItem.Sublist 属性将一个列表作为二级列表添加到该项目。
- 使用 PdfSortedList.Draw() 方法将列表绘制在页面的指定位置。
- 使用 PdfDocument.SaveToFile() 方法保存 PDF 文档。
- C#
- VB.NET
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;
namespace CreateMultiLevelLists
{
class Program
{
static void Main(string[] args)
{
//创建 PdfDocument 类的对象
PdfDocument doc = new PdfDocument();
//设置页边距
PdfMargins margin = new PdfMargins(30);
//添加一个页面
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
//设置初始坐标
float x = 10;
float y = 20;
//创建两个绘制参数
PdfBrush blackBrush = PdfBrushes.Black;
PdfBrush purpleBrush = PdfBrushes.Purple;
//创建两个字体
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("微软雅黑", 14f), true);
PdfTrueTypeFont listFont = new PdfTrueTypeFont(new Font("微软雅黑", 12f), true);
//为列表创建标记
PdfOrderedMarker marker = new PdfOrderedMarker(PdfNumberStyle.Numeric, listFont);
//绘制标题
String title = "下面展示的是一个多级编号列表:";
page.Canvas.DrawString(title, titleFont, blackBrush, x, y);
y = y + (float)titleFont.MeasureString(title).Height;
y = y + 5;
//创建一级列表
String parentListContent = "一级列表项目 1\n"
+ "一级列表项目 2";
PdfSortedList parentList = new PdfSortedList(parentListContent);
parentList.Font = listFont;
parentList.Indent = 2;
parentList.Brush = purpleBrush;
parentList.Marker = marker;
//创建第一个二级列表
String subListContent_1 = "二级列表项目 1\n"
+ "二级列表项目 2\n"
+ "二级列表项目 3\n"
+ "二级列表项目 4";
PdfSortedList subList_1 = new PdfSortedList(subListContent_1);
subList_1.Indent = 12;
subList_1.Font = listFont;
subList_1.Brush = blackBrush;
subList_1.Marker = marker;
subList_1.MarkerHierarchy = true;
//创建第二个二级列表
String subListContent_2 = "二级列表项目 1\n"
+ "二级列表项目 2\n"
+ "二级列表项目 3";
PdfSortedList subList_2 = new PdfSortedList(subListContent_2);
subList_2.Indent = 12;
subList_2.Font = listFont;
subList_2.Brush = blackBrush;
subList_2.Marker = marker;
subList_2.MarkerHierarchy = true;
//创建第一个三级列表
String subSubListContent_1 = "三级列表项目 1\n"
+ "三级列表项目 2";
PdfSortedList subSubList = new PdfSortedList(subSubListContent_1);
subSubList.Indent = 20;
subSubList.Font = listFont;
subSubList.Brush = blackBrush;
subSubList.Marker = marker;
subSubList.MarkerHierarchy = true;
//将第一个二级列表设置为一级列表第一个项目的子列表
PdfListItem item_1 = parentList.Items[0];
item_1.SubList = subList_1;
//将第二个二级列表设置为一级列表第二个项目的子列表
PdfListItem item_2 = parentList.Items[1];
item_2.SubList = subList_2;
//将三级列表设置为第一个二级列表的子列表
PdfListItem item_1_1 = subList_1.Items[0];
item_1_1.SubList = subSubList;
//绘制列表
parentList.Draw(page, x, y);
//保存文档
doc.SaveToFile("多级编号列表.pdf");
}
}
}
Imports System
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Lists
Namespace CreateMultiLevelLists
Class Program
Shared Sub Main(ByVal args() As String)
'创建 PdfDocument 类的对象 Dim doc As PdfDocument = New PdfDocument() '设置页边距
Dim margin As PdfMargins = New PdfMargins(30)
'添加一个页面 Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4,margin) '设置初始坐标
Dim x As Single = 10
Dim y As Single = 20
'创建两个绘制参数 Dim blackBrush As PdfBrush = PdfBrushes.Black Dim purpleBrush As PdfBrush = PdfBrushes.Purple '创建两个字体
Dim titleFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("微软雅黑", 14.0F), True)
Dim listFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("微软雅黑", 12.0F), True)
'为列表创建标记 Dim marker As PdfOrderedMarker = New PdfOrderedMarker(PdfNumberStyle.Numeric,listFont) '绘制标题
Dim title As String = "下面展示的是一个多级编号列表:"
page.Canvas.DrawString(title, titleFont, blackBrush, x, y)
y = y + CType(titleFont.MeasureString(title).Height, Single)
y = y + 5
'创建一级列表 Dim parentListContent As String = "一级列表项目 1\n" + "一级列表项目 2" Dim parentList As PdfSortedList = New PdfSortedList(parentListContent) parentList.Font = listFont parentList.Indent = 2 parentList.Brush = purpleBrush parentList.Marker = marker '创建第一个二级列表
Dim subListContent_1 As String = "二级列表项目 1\n" +
"二级列表项目 2\n" +
"二级列表项目 3\n" +
"二级列表项目 4"
Dim subList_1 As PdfSortedList = New PdfSortedList(subListContent_1)
subList_1.Indent = 12
subList_1.Font = listFont
subList_1.Brush = blackBrush
subList_1.Marker = marker
subList_1.MarkerHierarchy = True
'创建第二个二级列表 Dim subListContent_2 As String = "二级列表项目 1\n" + "二级列表项目 2\n" + "二级列表项目 3" Dim subList_2 As PdfSortedList = New PdfSortedList(subListContent_2) subList_2.Indent = 12 subList_2.Font = listFont subList_2.Brush = blackBrush subList_2.Marker = marker subList_2.MarkerHierarchy = True '创建第一个三级列表
Dim subSubListContent_1 As String = "三级列表项目 1\n" +
"三级列表项目 2"
Dim subSubList As PdfSortedList = New PdfSortedList(subSubListContent_1)
subSubList.Indent = 20
subSubList.Font = listFont
subSubList.Brush = blackBrush
subSubList.Marker = marker
subSubList.MarkerHierarchy = True
'将第一个二级列表设置为一级列表第一个项目的子列表 Dim item_1 As PdfListItem = parentList.Items(0) item_1.SubList = subList_1 '将第二个二级列表设置为一级列表第二个项目的子列表
Dim item_2 As PdfListItem = parentList.Items(1)
item_2.SubList = subList_2
'将三级列表设置为第一个二级列表的子列表 Dim item_1_1 As PdfListItem = subList_1.Items(0) item_1_1.SubList = subSubList '绘制列表
parentList.Draw(page, x, y)
'保存文档 doc.SaveToFile("多级编号列表.pdf") End Sub End Class End Namespace
在 PDF 中创建以符号为项目符号的无序列表
无序列表,也叫项目符号列表,是没有特别顺序的项目的集合。无需列表的每个项目都以项目符号标记。以下是使用 Spire.PDF for .NET 在 PDF 中创建以符号为项目符号的无序列表的详细操作步骤。
- 创建 PdfDocument类的对象。
- 使用 PdfDocument.Pages.Add()方法在文档中添加一个页面。
- 为列表创建 PdfBrush 类的对象和 PdfFont类的对象。
- 创建 PdfMarker类的对象并指定标记样式。
- 用字符串指定列表内容并基于字符串创建 PdfList类的对象。
- 通过 PdfList类下的属性设置字体、缩进、绘制和标记等参数。
- 使用 PdfList.Draw()方法将列表绘制在页面的指定位置。
- 使用 PdfDocument.SaveToFile()方法保存 PDF 文档。
- C#
- VB.NET
using System;using System.Drawing;using Spire.Pdf;using Spire.Pdf.Graphics;using Spire.Pdf.Lists;namespace ChangePDFPageSize{internal class Program{static void Main(string[] args){//创建 PdfDocument 类的对象 PdfDocument doc = new PdfDocument();//设置页边距 PdfMargins margin = new PdfMargins(30);//添加一个页面 PdfPageBase page = doc.Pages.Add(PdfPageSize.A4,margin);//创建字体 PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("微软雅黑",14f),true);PdfTrueTypeFont listFont = new PdfTrueTypeFont(new Font("微软雅黑",12f),true);PdfTrueTypeFont markerFont = new PdfTrueTypeFont(new Font("微软雅黑",6f),true);//创建 PdfBrush 类的对象 PdfBrush brush = PdfBrushes.Black;//设置初始坐标 float x = 10;float y = 20;//绘制标题 String title = "计算机科学主题:";page.Canvas.DrawString(title,titleFont,brush,x,y);y = y + (float)titleFont.MeasureString(title).Height;y = y + 5;//指定标记样式 PdfMarker marker = new PdfMarker(PdfUnorderedMarkerStyle.Asterisk);marker.Font = markerFont;//创建无序列表 String listContent = "数据结构\n" + "算法\n" + "计算机网络\n" + "操作系统\n" + "计算理论\n" + "C语言编程\n" + "计算机组织和架构";PdfList list = new PdfList(listContent);//设置字体、段落缩进、文本缩进、绘制等参数 list.Font = listFont;list.Indent = 2;list.TextIndent = 4;list.Brush = brush;list.Marker = marker;//将列表绘制在页面指定位置 list.Draw(page,x,y);//保存文档 doc.SaveToFile("无序列表.pdf");}}}
Imports System Imports System.Drawing Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports Spire.Pdf.Lists Namespace ChangePDFPageSize Friend Class Program Shared Sub Main(ByVal args() As String) '创建 PdfDocument 类的对象
Dim doc As PdfDocument = New PdfDocument()
'设置页边距 Dim margin As PdfMargins = New PdfMargins(30) '添加一个页面
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin)
'创建字体 Dim titleFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("微软雅黑",14.0F),True) Dim listFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("微软雅黑",12.0F),True) Dim markerFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("微软雅黑",6.0F),True) '创建 PdfBrush 类的对象
Dim brush As PdfBrush = PdfBrushes.Black
'设置初始坐标 Dim x As Single = 10 Dim y As Single = 20 '绘制标题
Dim title As String = "计算机科学主题:"
page.Canvas.DrawString(title, titleFont, brush, x, y)
y = y + CType(titleFont.MeasureString(title).Height, Single)
y = y + 5
'指定标记样式 Dim marker As PdfMarker = New PdfMarker(PdfUnorderedMarkerStyle.Asterisk) marker.Font = markerFont '创建无序列表
Dim listContent As String = "数据结构\n" +
"算法\n" +
"计算机网络\n" +
"操作系统\n" +
"计算理论\n" +
"C语言编程\n" +
"计算机组织和架构"
Dim list As PdfList = New PdfList(listContent)
'设置字体、段落缩进、文本缩进、绘制等参数 list.Font = listFont list.Indent = 2 list.TextIndent = 4 list.Brush = brush list.Marker = marker '将列表绘制在页面指定位置
list.Draw(page, x, y)
'保存文档 doc.SaveToFile("无序列表.pdf") End Sub End Class End Namespace
在 PDF 中创建以图片为项目符号的无序列表
除了使用符号以外,还可以使用图片作为项目符号。创建以图片为项目符号的无序列表的详细操作步骤如下。
- 创建 PdfDocument类的对象。
- 使用 PdfDocument.Pages.Add()方法在文档中添加一个页面。
- 为列表创建 PdfBrush类的对象和 PdfFont类的对象。
- 创建 PdfMarker类的对象并设置标记样式为 CustomImage。
- 使用 PdfMarker.Image方法将标记设置为图片。
- 用字符串指定列表内容并基于字符串创建 PdfList类的对象。
- 通过 PdfList类下的属性设置字体、缩进、绘制和标记等参数。
- 使用 PdfList.Draw()方法将列表绘制在页面的指定位置。
- 使用 PdfDocument.SaveToFile()方法保存 PDF 文档。
- C#
- VB.NET
using System;using System.Drawing;using Spire.Pdf;using Spire.Pdf.Graphics;using Spire.Pdf.Lists;namespace ChangePDFPageSize{internal class Program{static void Main(string[] args){//创建 PdfDocument 类的对象 PdfDocument doc = new PdfDocument();//设置页边距 PdfMargins margin = new PdfMargins(30);//添加一个页面 PdfPageBase page = doc.Pages.Add(PdfPageSize.A4,margin);//创建字体 PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("微软雅黑",14f),true);PdfTrueTypeFont listFont = new PdfTrueTypeFont(new Font("微软雅黑",12f),true);//创建 PdfBrush 类的对象 PdfBrush brush = PdfBrushes.Black;//设置初始坐标 float x = 10;float y = 20;//绘制标题 String title = "项目任务待完成列表:";page.Canvas.DrawString(title,titleFont,brush,x,y);y = y + (float)titleFont.MeasureString(title).Height;y = y + 5;//指定标记样式为图片 PdfMarker marker = new PdfMarker(PdfUnorderedMarkerStyle.CustomImage);//设置标记图片 marker.Image = PdfImage.FromFile(@"C:\项目符号.png");//创建无序列表 String listContent = "确定正在进行的项目和任务\n" + "分配任务\n" + "确定任务优先级\n" + "追踪任务进展状况\n" + "任务完成后标记为已完成\n";PdfList list = new PdfList(listContent);//设置字体、段落缩进、文本缩进、绘制等参数 list.Font = listFont;list.Indent = 2;list.TextIndent = 4;list.Brush = brush;list.Marker = marker;//将列表绘制在页面指定位置 list.Draw(page,x,y);//保存文档 doc.SaveToFile("无序列表.pdf");}}}
Imports System Imports System.Drawing Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports Spire.Pdf.Lists Namespace ChangePDFPageSize Friend Class Program Shared Sub Main(ByVal args() As String) '创建 PdfDocument 类的对象
Dim doc As PdfDocument = New PdfDocument()
'设置页边距 Dim margin As PdfMargins = New PdfMargins(30) '添加一个页面
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin)
'创建字体 Dim titleFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("微软雅黑",14.0F),True) Dim listFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("微软雅黑",12.0F),True) '创建 PdfBrush 类的对象
Dim brush As PdfBrush = PdfBrushes.Black
'设置初始坐标 Dim x As Single = 10 Dim y As Single = 20 '绘制标题
Dim title As String = "项目任务待完成列表:"
page.Canvas.DrawString(title, titleFont, brush, x, y)
y = y + CType(titleFont.MeasureString(title).Height, Single)
y = y + 5
'指定标记样式为图片 Dim marker As PdfMarker = New PdfMarker(PdfUnorderedMarkerStyle.CustomImage) '设置标记图片
marker.Image = PdfImage.FromFile("C:\项目符号.png")
'创建无序列表 Dim listContent As String = "确定正在进行的项目和任务\n" + "分配任务\n" + "确定任务优先级\n" + "追踪任务进展状况\n" + "任务完成后标记为已完成\n" Dim list As PdfList = New PdfList(listContent) '设置字体、段落缩进、文本缩进、绘制等参数
list.Font = listFont
list.Indent = 2
list.TextIndent = 4
list.Brush = brush
list.Marker = marker
'将列表绘制在页面指定位置 list.Draw(page,x,y) '保存文档
doc.SaveToFile("无序列表.pdf")
End Sub
End Class
End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。