当前位置: 代码网 > it编程>编程语言>Asp.net > C#代码实现在PDF文档中创建列表

C#代码实现在PDF文档中创建列表

2026年08月23日 Asp.net 我要评论
在 pdf 文档中,列表是一种将相关内容按照一定顺序排列展示的方式。合理使用列表,可以帮助读者快速获取关键信息、清晰展示操作步骤,或对内容进行结构化概览。本文将介绍如何使用 c# 和 vb.net 在

在 pdf 文档中,列表是一种将相关内容按照一定顺序排列展示的方式。合理使用列表,可以帮助读者快速获取关键信息、清晰展示操作步骤,或对内容进行结构化概览。

本文将介绍如何使用 c# 和 vb.net 在 pdf 文档中创建有序列表和无序列表,帮助开发者更方便地组织和呈现文档内容。

安装 pdf 处理组件

开始之前,需要先将 pdf 处理所需的 dll 文件添加到 .net 项目中作为引用。相关程序集可以通过下载对应的开发包获取,也可以直接通过 nuget 包管理器进行安装。

如果使用 nuget 安装,可在项目中搜索并添加相应的 pdf 处理库,即可完成环境配置。

pm> install-package spire.pdf

在 pdf 中创建有序列表

有序列表是一种按照固定顺序排列内容的展示方式,列表中的每一项通常会使用数字、字母或罗马数字作为标识。通过有序列表,可以让文档内容更加清晰,方便读者理解信息层级和操作流程。

以下是在 pdf 文档中创建有序列表的基本步骤:

  • 创建一个 pdf 文档对象。
  • 添加新的页面用于显示列表内容。
  • 设置列表所需的字体和画刷样式。
  • 创建列表标记对象,并指定编号样式(如数字、字母或罗马数字)。
  • 定义列表文本内容,并根据内容创建列表对象。
  • 设置列表的字体、缩进、颜色以及编号样式等属性。
  • 将列表绘制到 pdf 页面中的指定位置。
  • 保存生成的 pdf 文件。

完整示例代码如下:

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 文件。

完整示例代码如下:

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 文件。

完整示例代码如下:

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 文件。

完整示例代码如下:

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 列表的创建方法后,开发者可以根据实际需求设计更符合业务场景的文档布局,例如报告、说明文档、任务清单以及结构化资料等。

到此这篇关于c#代码实现在pdf文档中创建列表的文章就介绍到这了,更多相关c# pdf创建列表内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com