在 PDF 文档中,列表是一种将相关内容按照一定顺序排列展示的方式。合理使用列表,可以帮助读者快速获取关键信息、清晰展示操作步骤,或对内容进行结构化概览。
本文将介绍如何使用 C# 和 VB.NET 在 PDF 文档中创建有序列表和无序列表,帮助开发者更方便地组织和呈现文档内容。
安装 PDF 处理组件
开始之前,需要先将 PDF 处理所需的 DLL 文件添加到 .NET 项目中作为引用。相关程序集可以通过下载对应的开发包获取,也可以直接通过 NuGet 包管理器进行安装。
如果使用 NuGet 安装,可在项目中搜索并添加相应的 PDF 处理库,即可完成环境配置。
cs
PM> Install-Package Spire.PDF
在 PDF 中创建有序列表
有序列表是一种按照固定顺序排列内容的展示方式,列表中的每一项通常会使用数字、字母或罗马数字作为标识。通过有序列表,可以让文档内容更加清晰,方便读者理解信息层级和操作流程。
以下是在 PDF 文档中创建有序列表的基本步骤:
- 创建一个 PDF 文档对象。
- 添加新的页面用于显示列表内容。
- 设置列表所需的字体和画刷样式。
- 创建列表标记对象,并指定编号样式(如数字、字母或罗马数字)。
- 定义列表文本内容,并根据内容创建列表对象。
- 设置列表的字体、缩进、颜色以及编号样式等属性。
- 将列表绘制到 PDF 页面中的指定位置。
- 保存生成的 PDF 文件。
完整示例代码如下:
cs
using System;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;
namespace CreateOrderedList
{
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 brush = PdfBrushes.Black;
// 创建字体
PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold);
PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);
// 创建有序列表标记,并设置编号样式
PdfOrderedMarker marker = new PdfOrderedMarker(PdfNumberStyle.LowerLatin, listFont);
// 设置初始坐标位置
float x = 10;
float y = 20;
// 绘制标题
String title = "Required Web Development Skills:";
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);
// 保存 PDF 文件
doc.SaveToFile("OrderedList.pdf");
}
}
}
在 PDF 中创建带项目符号的无序列表
无序列表(也称为项目符号列表)是一种用于展示相关内容的列表形式,列表项之间没有固定的排列顺序,每一项通常会使用符号作为标识。
通过无序列表,可以让文档中的重点内容更加突出,提高信息的可读性。以下是在 PDF 文档中创建无序列表的基本步骤:
- 创建 PDF 文档对象。
- 添加页面用于显示列表内容。
- 设置列表所需的字体和画刷样式。
- 创建列表标记对象,并指定项目符号样式。
- 定义列表内容,并根据文本内容创建列表对象。
- 设置列表的字体、缩进、颜色以及项目符号样式等属性。
- 将列表绘制到 PDF 页面中的指定位置。
- 保存生成的 PDF 文件。
完整示例代码如下:
cs
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;
using System;
namespace CreateBulletedList
{
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);
// 创建字体
PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold);
PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);
PdfFont markerFont = new PdfFont(PdfFontFamily.TimesRoman, 6f, PdfFontStyle.Regular);
// 创建画刷
PdfBrush brush = PdfBrushes.Black;
// 设置初始坐标位置
float x = 10;
float y = 20;
// 绘制标题
String title = "Computer Science Subjects:";
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 = "Data Structure\n"
+ "Algorithm\n"
+ "Computer Networks\n"
+ "Operating System\n"
+ "Theory of Computations\n"
+ "C Programming\n"
+ "Computer Organization and Architecture";
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);
// 保存 PDF 文件
doc.SaveToFile("UnorderedList.pdf");
}
}
}
在 PDF 中创建带图片项目符号的无序列表
除了使用普通符号作为项目符号外,无序列表还可以使用图片作为列表标记。通过自定义图片,可以让列表展示效果更加直观,也能满足更多个性化排版需求。
创建带图片项目符号的无序列表主要步骤如下:
- 创建 PDF 文档对象。
- 添加页面用于显示列表内容。
- 设置列表所需的字体和画刷样式。
- 创建列表标记对象,并将标记类型设置为自定义图片。
- 通过图片属性设置项目符号所使用的图片。
- 定义列表文本内容,并根据内容创建列表对象。
- 设置列表的字体、缩进、颜色以及图片标记等属性。
- 将列表绘制到 PDF 页面中的指定位置。
- 保存生成的 PDF 文件。
完整示例代码如下:
cs
using System;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;
namespace CustomizeBulletPointsWithImage
{
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);
// 创建字体
PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold);
PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);
// 创建画刷
PdfBrush brush = PdfBrushes.Black;
// 设置初始坐标位置
float x = 10;
float y = 20;
// 绘制标题
String title = "Project Task To-Do List:";
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:\Users\Administrator\Desktop\checkmark.jpg");
// 创建无序列表
String listContent = "Define projects and tasks you're working on\n"
+ "Assign people to tasks\n"
+ "Define the priority levels of your tasks\n"
+ "Keep track of the progress status of your tasks\n"
+ "Mark tasks as done when completed";
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);
// 保存 PDF 文件
doc.SaveToFile("ImageBullets.pdf");
}
}
}
在 PDF 中创建嵌套编号列表
嵌套列表是一种包含子列表的列表结构,通常用于展示具有层级关系的信息,例如目录结构、分类内容或多级任务列表。通过嵌套列表,可以让文档内容更加清晰,方便读者理解不同层级之间的关系。
在 PDF 文档中创建嵌套编号列表的主要步骤如下:
- 创建 PDF 文档对象。
- 添加页面用于显示列表内容。
- 创建编号标记对象,并设置编号样式(如数字编号)。
- 定义列表内容,并根据内容创建父级列表,然后设置列表的字体、缩进、颜色以及编号样式等属性。
- 按照相同方式创建子列表和更深层级的嵌套列表。
- 获取父级列表中的指定列表项,并将子列表添加到对应项目下。
- 将嵌套列表绘制到 PDF 页面中的指定位置。
- 保存生成的 PDF 文件。
完整示例代码如下:
cs
using System;
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;
// 创建两个字体
PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold);
PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);
// 创建编号列表标记
PdfOrderedMarker marker = new PdfOrderedMarker(PdfNumberStyle.Numeric, listFont);
// 绘制标题
String title = "Below is a Nested Numbered List:";
page.Canvas.DrawString(title, titleFont, blackBrush, x, y);
y = y + (float)titleFont.MeasureString(title).Height;
y = y + 5;
// 创建父级列表
String parentListContent = "Parent Item 1\n"
+ "Parent Item 2";
PdfSortedList parentList = new PdfSortedList(parentListContent);
parentList.Font = listFont;
parentList.Indent = 2;
parentList.Brush = purpleBrush;
parentList.Marker = marker;
// 创建子列表 subList_1
String subListContent_1 = "Sub Item 1\n"
+ "Sub Item 2\n"
+ "Sub Item 3\n"
+ "Sub Item 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;
// 创建另一个子列表 subList_2
String subListContent_2 = "Sub Item 1\n"
+ "Sub Item 2\n"
+ "Sub Item 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;
// 创建子子列表 subSubList
String subSubListContent_1 = "Sub Sub Item 1\n"
+ "Sub Sub Item 2";
PdfSortedList subSubList = new PdfSortedList(subSubListContent_1);
subSubList.Indent = 20;
subSubList.Font = listFont;
subSubList.Brush = blackBrush;
subSubList.Marker = marker;
subSubList.MarkerHierarchy = true;
// 将 subList_1 添加为父级列表第一个项目的子列表
PdfListItem item_1 = parentList.Items[0];
item_1.SubList = subList_1;
// 将 subList_2 添加为父级列表第二个项目的子列表
PdfListItem item_2 = parentList.Items[1];
item_2.SubList = subList_2;
// 将 subSubList 添加为 subList_1 第一个项目的子列表
PdfListItem item_1_1 = subList_1.Items[0];
item_1_1.SubList = subSubList;
// 绘制父级列表
parentList.Draw(page, x, y);
// 保存 PDF 文件
doc.SaveToFile("MultiLevelList.pdf");
}
}
}
总结
本文介绍了如何在 PDF 文档中创建不同类型的列表,包括有序列表、无序列表、带图片项目符号的列表以及多级嵌套列表。通过合理使用列表结构,可以帮助开发者更清晰地组织文档内容,提高 PDF 文件的可读性和信息展示效果。
在 .NET 开发环境中,可以通过设置列表样式、编号格式、项目符号、字体、缩进等属性,实现更加灵活的 PDF 排版效果。无论是简单的信息罗列,还是具有层级关系的复杂内容展示,都可以通过列表功能快速完成。
掌握 PDF 列表的创建方法后,开发者可以根据实际需求设计更符合业务场景的文档布局,例如报告、说明文档、任务清单以及结构化资料等。