查看: 1356|回复: 0

[ASP.NET教程] C# /VB.NET 创建PDF项目符号列表和多级编号列表

发表于 2018-4-25 08:00:02

使用项目符号和编号,可以让文档的层次结构更清晰、更有条理,也更容易突出重点。在编辑文档的过程中,我个人也比较偏爱项目标号来标注文章重点信息。在之前的文章中,介绍了如何在Word中来创建项目标号和编号列表,在本篇文章中,将介绍创建PDF项目符号列表和多级编号列表的方法。

借助工具: Spire.PDF.dll

PS:dll可以直接在安装路径下的Bin文件夹中获取。

1.创建PDF符号列表

C#

  1. using System;
  2. using System.Drawing;
  3. using Spire.Pdf;
  4. using Spire.Pdf.Graphics;
  5. using Spire.Pdf.Lists;
  6. namespace CreateList_PDF
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //创建一个PDF文档
  13. PdfDocument doc = new PdfDocument();
  14. //添加一页A4大小的页面
  15. PdfPageBase page = doc.Pages.Add(PdfPageSize.A4);
  16. float y = 10;
  17. //创建标题并设置格式,包括字体、字号、颜色
  18. PdfBrush brush1 = PdfBrushes.Black;
  19. PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("楷体", 18f, FontStyle.Regular), true);
  20. PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
  21. page.Canvas.DrawString("Categories List(项目列表)", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
  22. y = y + font1.MeasureString("Categories List(项目列表)", format1).Height;
  23. y = y + 5;
  24. //创建列表文字并设置格式
  25. RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize);
  26. PdfLinearGradientBrush brush
  27. = new PdfLinearGradientBrush(rctg, Color.SteelBlue, Color.Blue, PdfLinearGradientMode.Vertical);
  28. PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("宋体", 15f, FontStyle.Regular), true);
  29. String formatted
  30. = "Part 1 Listening(听力部分) \n Part 2 Speaking(口语部分) \n Part 3 Reading(阅读部分) \n Part 4 Writing(写作部分)"
  31. + "\n Part 5 Test(测试) 1 \n Part 6 Test(测试) 2 \n Part 7 Test Answers(测试答案)";
  32. //创建PdfList 类对象,并设置列表及样式
  33. PdfList list = new PdfList(formatted);
  34. list.Font = font2;
  35. list.Indent = 8;
  36. list.TextIndent = 5;
  37. list.Brush = brush;
  38. PdfLayoutResult result = list.Draw(page, 0, y);
  39. y = result.Bounds.Bottom;
  40. //保存并打开文档
  41. doc.SaveToFile("List.pdf");
  42. System.Diagnostics.Process.Start("List.pdf");
  43. }
  44. }
  45. }
复制代码

测试结果:

VB.NET

  1. Imports System
  2. Imports System.Drawing
  3. Imports Spire.Pdf
  4. Imports Spire.Pdf.Graphics
  5. Imports Spire.Pdf.Lists
  6. Namespace CreateList_PDF
  7. Class Program
  8. Private Shared Sub Main(ByVal args As String())
  9. Dim doc As PdfDocument = New PdfDocument()
  10. Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4)
  11. Dim y As Single = 10
  12. Dim brush1 As PdfBrush = PdfBrushes.Black
  13. Dim font1 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("楷体", 18F, FontStyle.Regular), True)
  14. Dim format1 As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Center)
  15. page.Canvas.DrawString("Categories List(项目列表)", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1)
  16. y = y + font1.MeasureString("Categories List(项目列表)", format1).Height
  17. y = y + 5
  18. Dim rctg As RectangleF = New RectangleF(New PointF(0, 0), page.Canvas.ClientSize)
  19. Dim brush As PdfLinearGradientBrush = New PdfLinearGradientBrush(rctg, Color.SteelBlue, Color.Blue, PdfLinearGradientMode.Vertical)
  20. Dim font2 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("宋体", 15F, FontStyle.Regular), True)
  21. 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(测试答案)"
  22. Dim list As PdfList = New PdfList(formatted)
  23. list.Font = font2
  24. list.Indent = 8
  25. list.TextIndent = 5
  26. list.Brush = brush
  27. Dim result As PdfLayoutResult = list.Draw(page, 0, y)
  28. y = result.Bounds.Bottom
  29. doc.SaveToFile("List.pdf")
  30. System.Diagnostics.Process.Start("List.pdf")
  31. End Sub
  32. End Class
  33. End Namespace
复制代码
2.创建PDF多级编号列表

Spire.Pdf.dll支持多种类型的编号类型:

C#

  1. using Spire.Pdf;
  2. using Spire.Pdf.Graphics;
  3. using Spire.Pdf.Lists;
  4. using System;
  5. using System.Drawing;
  6. namespace List2
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //新建PDF文档,并添加一页A4大小的空白页面
  13. PdfDocument doc = new PdfDocument();
  14. PdfPageBase page = doc.Pages.Add(PdfPageSize.A4);
  15. float y = 10;
  16. //添加标题并设置文本和格式
  17. PdfBrush brush1 = PdfBrushes.Black;
  18. PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("宋体", 18f, FontStyle.Regular), true);
  19. PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
  20. page.Canvas.DrawString("目录一览", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
  21. y = y + font1.MeasureString("目录一览", format1).Height;
  22. y = y + 5;
  23. //添加文本并设置字体
  24. RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize);
  25. PdfLinearGradientBrush brush
  26. = new PdfLinearGradientBrush(rctg, Color.DarkBlue, Color.Brown, PdfLinearGradientMode.Vertical);
  27. PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("宋体", 15f, FontStyle.Regular), true);
  28. String formatted1 = "第一章 总 论";
  29. String formatted2 = "第一节 经济法概述 ";
  30. String formatted3 = "第二节 经济法主体 ";
  31. String formatted4 = "第二章 公司法律制度";
  32. //设置编号列表样式
  33. PdfOrderedMarker marker1 = new PdfOrderedMarker(PdfNumberStyle.Numeric, new PdfFont(PdfFontFamily.Helvetica, 15f));
  34. PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.LowerRoman, new PdfFont(PdfFontFamily.Helvetica, 12f));
  35. //为第一级编号列表设置格式
  36. PdfSortedList list1 = new PdfSortedList(font2);
  37. list1.Indent = 0;
  38. list1.TextIndent = 5;
  39. list1.Brush = brush;
  40. list1.Marker = marker1;
  41. //为第二级编号列表设置格式
  42. PdfSortedList list2 = new PdfSortedList(font2);
  43. list2.Marker = marker2;
  44. list2.Brush = brush;
  45. list2.TextIndent = 10;
  46. //应用编号列表格式到指定文本
  47. PdfListItem item = list1.Items.Add(formatted1);
  48. item.SubList = list2;
  49. list2.Items.Add(formatted2);
  50. list2.Items.Add(formatted3);
  51. list1.Items.Add(formatted4);
  52. //设置PDF文档布局和位置
  53. PdfTextLayout textLayout = new PdfTextLayout();
  54. textLayout.Break = PdfLayoutBreakType.FitPage;
  55. textLayout.Layout = PdfLayoutType.Paginate;
  56. list1.Draw(page, new PointF(0, y), textLayout);
  57. //保存文件
  58. doc.SaveToFile("多级列表.pdf");
  59. System.Diagnostics.Process.Start("多级列表.pdf");
  60. }
  61. }
  62. }
复制代码

测试结果:

VB.NET

  1. Imports Spire.Pdf
  2. Imports Spire.Pdf.Graphics
  3. Imports Spire.Pdf.Lists
  4. Imports System
  5. Imports System.Drawing
  6. Namespace List2
  7. Class Program
  8. Private Shared Sub Main(ByVal args As String())
  9. Dim doc As PdfDocument = New PdfDocument()
  10. Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4)
  11. Dim y As Single = 10
  12. Dim brush1 As PdfBrush = PdfBrushes.Black
  13. Dim font1 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("宋体", 18F, FontStyle.Regular), True)
  14. Dim format1 As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Center)
  15. page.Canvas.DrawString("目录一览", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1)
  16. y = y + font1.MeasureString("目录一览", format1).Height
  17. y = y + 5
  18. Dim rctg As RectangleF = New RectangleF(New PointF(0, 0), page.Canvas.ClientSize)
  19. Dim brush As PdfLinearGradientBrush = New PdfLinearGradientBrush(rctg, Color.DarkBlue, Color.Brown, PdfLinearGradientMode.Vertical)
  20. Dim font2 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("宋体", 15F, FontStyle.Regular), True)
  21. Dim formatted1 As String = "第一章 总 论"
  22. Dim formatted2 As String = "第一节 经济法概述 "
  23. Dim formatted3 As String = "第二节 经济法主体 "
  24. Dim formatted4 As String = "第二章 公司法律制度"
  25. Dim marker1 As PdfOrderedMarker = New PdfOrderedMarker(PdfNumberStyle.Numeric, NewPdfFont(PdfFontFamily.Helvetica, 15F))
  26. Dim marker2 As PdfOrderedMarker = New PdfOrderedMarker(PdfNumberStyle.LowerRoman, NewPdfFont(PdfFontFamily.Helvetica, 12F))
  27. Dim list1 As PdfSortedList = New PdfSortedList(font2)
  28. list1.Indent = 0
  29. list1.TextIndent = 5
  30. list1.Brush = brush
  31. list1.Marker = marker1
  32. Dim list2 As PdfSortedList = New PdfSortedList(font2)
  33. list2.Marker = marker2
  34. list2.Brush = brush
  35. list2.TextIndent = 10
  36. Dim item As PdfListItem = list1.Items.Add(formatted1)
  37. item.SubList = list2
  38. list2.Items.Add(formatted2)
  39. list2.Items.Add(formatted3)
  40. list1.Items.Add(formatted4)
  41. Dim textLayout As PdfTextLayout = New PdfTextLayout()
  42. textLayout.Break = PdfLayoutBreakType.FitPage
  43. textLayout.Layout = PdfLayoutType.Paginate
  44. list1.Draw(page, New PointF(0, y), textLayout)
  45. doc.SaveToFile("多级列表.pdf")
  46. System.Diagnostics.Process.Start("多级列表.pdf")
  47. End Sub
  48. End Class
  49. End Namespace
复制代码

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



回复

使用道具 举报