使用項目符號和編號,可以讓文檔的層次結構更清晰、更有條理,也更容易突出重點。在編輯文檔的過程中,我個人也比較偏愛項目標號來標註文章重點信息。在之前的文章中,介紹瞭如何在Word中來創建項目標號和編號列表,在本篇文章中,將介紹創建PDF項目符號列表和多級編號列表的方法。 藉助工具: Spire.PDF ...
使用項目符號和編號,可以讓文檔的層次結構更清晰、更有條理,也更容易突出重點。在編輯文檔的過程中,我個人也比較偏愛項目標號來標註文章重點信息。在之前的文章中,介紹瞭如何在Word中來創建項目標號和編號列表,在本篇文章中,將介紹創建PDF項目符號列表和多級編號列表的方法。
藉助工具: Spire.PDF.dll
PS:dll可以直接在安裝路徑下的Bin文件夾中獲取。
1.創建PDF符號列表
C#
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 brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("楷體", 18f, FontStyle.Regular), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Categories List(項目列表)", font1, brush1, 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"); } } }
測試結果:
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 brush1 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, brush1, 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#
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 brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("宋體", 18f, FontStyle.Regular), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("目錄一覽", font1, brush1, 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"); } } }
測試結果:
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 brush1 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, brush1, 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項目標號列表和多級編號列表”的全部內容。如需轉載,請註明出處。感謝閱讀!