189 8069 5689

C#/VB.NET创建PDF项目符号列表和多级编号列表

使用项目符号和编号,可以让文档的层次结构更清晰、更有条理,也更容易突出重点。在编辑文档的过程中,我个人也比较偏爱项目标号来标注文章重点信息。在之前的文章中,介绍了如何在Word中来创建项目标号和编号列表,在本篇文章中,将介绍创建PDF项目符号列表和多级编号列表的方法。
借助工具:Free Spire.PDF for .NET
PS:dll可以直接在安装路径下的Bin文件夹中获取。

10年积累的成都做网站、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站策划后付款的网站建设流程,更有东兴免费网站建设让你可以放心的选择与我们合作。

1.创建PDF符号列表

Csharp

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;

namespace CreateList_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个PDF文档
            PdfDocument doc = new PdfDocument();
            //添加一页A4大小的页面
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4);
            float y = 10;

            //创建标题并设置格式,包括字体、字号、颜色
            PdfBrush brush2 = PdfBrushes.Black;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("楷体", 18f, FontStyle.Regular), true);
            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
            page.Canvas.DrawString("Categories List(项目列表)", font1, brush2, page.Canvas.ClientSize.Width / 2, y, format1);
            y = y + font1.MeasureString("Categories List(项目列表)", format1).Height;
            y = y + 5;

            //创建列表文字并设置格式
            RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize);
            PdfLinearGradientBrush brush
                = new PdfLinearGradientBrush(rctg, Color.SteelBlue, Color.Blue, PdfLinearGradientMode.Vertical);
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("宋体", 15f, FontStyle.Regular), true);
            String formatted
                = "Part 1 Listening(听力部分) \n Part 2 Speaking(口语部分) \n Part 3 Reading(阅读部分) \n Part 4 Writing(写作部分)"
                + "\n Part 5 Test(测试) 1 \n Part 6 Test(测试) 2 \n Part 7 Test Answers(测试答案)";

            //创建PdfList 类对象,并设置列表及样式
            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("List.pdf");
            System.Diagnostics.Process.Start("List.pdf");
        }
    }
}

测试结果:

C#/VB.NET 创建PDF项目符号列表和多级编号列表

VB.NET

Imports System
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Lists

Namespace CreateList_PDF

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim doc As PdfDocument = New PdfDocument()
            Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4)
            Dim y As Single = 10
            Dim brush2 As PdfBrush = PdfBrushes.Black
            Dim font1 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("楷体", 18F, FontStyle.Regular), True)
            Dim format1 As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Center)
            page.Canvas.DrawString("Categories List(项目列表)", font1, brush2, page.Canvas.ClientSize.Width / 2, y, format1)
            y = y + font1.MeasureString("Categories List(项目列表)", format1).Height
            y = y + 5
            Dim rctg As RectangleF = New RectangleF(New PointF(0, 0), page.Canvas.ClientSize)
            Dim brush As PdfLinearGradientBrush = New PdfLinearGradientBrush(rctg, Color.SteelBlue, Color.Blue, PdfLinearGradientMode.Vertical)
            Dim font2 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("宋体", 15F, FontStyle.Regular), True)
            Dim formatted As String = "Part 1 Listening(听力部分) " & vbLf & " Part 2 Speaking(口语部分) " & vbLf & " Part 3 Reading(阅读部分) " & vbLf & " Part 4 Writing(写作部分)" & vbLf & " Part 5 Test(测试) 1 " & vbLf & " Part 6 Test(测试) 2 " & vbLf & " Part 7 Test Answers(测试答案)"
            Dim list As PdfList = 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("List.pdf")
            System.Diagnostics.Process.Start("List.pdf")
        End Sub
    End Class
End Namespace

2.创建PDF多级编号列表

Spire.Pdf.dll支持多种类型的编号类型
C#/VB.NET 创建PDF项目符号列表和多级编号列表

Csharp

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;
using System;
using System.Drawing;

namespace List2
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建PDF文档,并添加一页A4大小的空白页面
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4);

            float y = 10;

            //添加标题并设置文本和格式
            PdfBrush brush2 = PdfBrushes.Black;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("宋体", 18f, FontStyle.Regular), true);
            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
            page.Canvas.DrawString("目录一览", font1, brush2, 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 = "第二节 经济法主体 ";
            String formatted4 = "第二章 公司法律制度";

            //设置编号列表样式
            PdfOrderedMarker marker1 = new PdfOrderedMarker(PdfNumberStyle.Numeric, new PdfFont(PdfFontFamily.Helvetica, 15f));
            PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.LowerRoman, new PdfFont(PdfFontFamily.Helvetica, 12f));

            //为第一级编号列表设置格式
            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);
            list1.Items.Add(formatted4);            

            //设置PDF文档布局和位置
            PdfTextLayout textLayout = new PdfTextLayout();
            textLayout.Break = PdfLayoutBreakType.FitPage;
            textLayout.Layout = PdfLayoutType.Paginate;
            list1.Draw(page, new PointF(0, y), textLayout);

            //保存文件
            doc.SaveToFile("多级列表.pdf");
            System.Diagnostics.Process.Start("多级列表.pdf");
        }
    }
}

测试结果:
C#/VB.NET 创建PDF项目符号列表和多级编号列表

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Lists
Imports System
Imports System.Drawing

Namespace List2

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim doc As PdfDocument = New PdfDocument()
            Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4)
            Dim y As Single = 10
            Dim brush2 As PdfBrush = PdfBrushes.Black
            Dim font1 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("宋体", 18F, FontStyle.Regular), True)
            Dim format1 As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Center)
            page.Canvas.DrawString("目录一览", font1, brush2, page.Canvas.ClientSize.Width / 2, y, format1)
            y = y + font1.MeasureString("目录一览", format1).Height
            y = y + 5
            Dim rctg As RectangleF = New RectangleF(New PointF(0, 0), page.Canvas.ClientSize)
            Dim brush As PdfLinearGradientBrush = New PdfLinearGradientBrush(rctg, Color.DarkBlue, Color.Brown, PdfLinearGradientMode.Vertical)
            Dim font2 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("宋体", 15F, FontStyle.Regular), True)
            Dim formatted1 As String = "第一章 总 论"
            Dim formatted2 As String = "第一节 经济法概述 "
            Dim formatted3 As String = "第二节 经济法主体 "
            Dim formatted4 As String = "第二章 公司法律制度"
            Dim marker1 As PdfOrderedMarker = New PdfOrderedMarker(PdfNumberStyle.Numeric, NewPdfFont(PdfFontFamily.Helvetica, 15F))
            Dim marker2 As PdfOrderedMarker = New PdfOrderedMarker(PdfNumberStyle.LowerRoman, NewPdfFont(PdfFontFamily.Helvetica, 12F))
            Dim list1 As PdfSortedList = New PdfSortedList(font2)
            list1.Indent = 0
            list1.TextIndent = 5
            list1.Brush = brush
            list1.Marker = marker1
            Dim list2 As PdfSortedList = 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)
            list1.Items.Add(formatted4)
            Dim textLayout As PdfTextLayout = New PdfTextLayout()
            textLayout.Break = PdfLayoutBreakType.FitPage
            textLayout.Layout = PdfLayoutType.Paginate
            list1.Draw(page, New PointF(0, y), textLayout)
            doc.SaveToFile("多级列表.pdf")
            System.Diagnostics.Process.Start("多级列表.pdf")
        End Sub
    End Class
End Namespace

以上内容为本次关于“创建PDF项目标号列表和多级编号列表”的全部内容。如需转载,请注明出处。感谢阅读!


本文标题:C#/VB.NET创建PDF项目符号列表和多级编号列表
当前URL:http://cdxtjz.cn/article/gespic.html

其他资讯