在 PDF 中创建目录 (TOC)
Spire.PDF for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。致力于在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档,而无需安装 Microsoft Word。
行号用于在每行文本旁边显示 Word 自动计算的行数。当我们需要参考合同或法律文件等文档中的特定行时,它非常有用。word中的行号功能允许我们设置起始值、编号间隔、与文本的距离以及行号的编号方式。使用 Spire.Doc,我们可以实现上述所有功能。本文将介绍如何将 HTML 转换为 PDF。
欢迎加入spire技术交流群:767755948
目录在提高文件的可读性和可浏览性方面起着至关重要的作用。它为读者提供了文档结构的清晰概览,使他们能够快速查找和访问感兴趣的特定章节或信息。这对于报告、书籍或学术论文等篇幅较长的文档尤为重要,因为读者可能需要多次查阅特定章节。在本文中,我们将探讨如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中创建 PDF 文档的目录。
安装 Spire.PDF for .NET
首先,您需要将 Spire.PDF for.NET 软件包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。DLL 文件既可以从这个链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.PDF在 C# 和 VB.NET 中创建 PDF 目录
目录主要包括 TOC 标题(如目录)、TOC 内容、页码以及点击后可进入目标页面的操作。使用 Spire.PDF for .NET 在 PDF 中创建目录的步骤如下:
- 初始化 PdfDocument 类的实例。
- 使用 PdfDocument.LoadFromFile() 方法加载 PDF 文档。
- 使用PdfDocument.Pages.Count属性获取文档页数。
- 使用PdfDocument.Pages.Insert(0)方法在PDF文档中插入新页作为第一页。
- 使用PdfPageBase.Canvas.DrawString()方法在页面上绘制TOC标题、TOC内容和页码。
- 使用PdfActionAnnotation类创建动作,并使用PdfNewPage.Annotations.Add()方法将动作添加到页面中。
- 使用PdfDocument.SaveToFile()方法保存结果文档。
using Spire.Pdf; using Spire.Pdf.Actions; using Spire.Pdf.Annotations; using Spire.Pdf.General; using Spire.Pdf.Graphics; using System; using System.Drawing; namespace TableOfContents { internal class Program { static void Main(string[] args) { //Initialize an instance of the PdfDocument class PdfDocument doc = new PdfDocument(); //Load a PDF document doc.LoadFromFile("Sample.PDF"); //Get the page count of the document int pageCount = doc.Pages.Count; //Insert a new page into the pdf document as the first page PdfPageBase tocPage = doc.Pages.Insert(0); //Draw TOC title on the new page string title = "Table of Contents"; PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Arial", 20, FontStyle.Bold)); PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); PointF location = new PointF(tocPage.Canvas.ClientSize.Width / 2, titleFont.MeasureString(title).Height + 10); tocPage.Canvas.DrawString(title, titleFont, PdfBrushes.CornflowerBlue, location, centerAlignment); //Draw TOC content on the new page PdfTrueTypeFont titlesFont = new PdfTrueTypeFont(new Font("Arial", 14)); String[] titles = new String[pageCount]; for (int i = 0; i < titles.Length; i++) { titles[i] = string.Format("This is page {0}", i + 1); } float y = titleFont.MeasureString(title).Height + 10; float x = 0; //Draw page numbers of the target pages on the new page for (int i = 1; i <= pageCount; i++) { string text = titles[i - 1]; SizeF titleSize = titlesFont.MeasureString(text); PdfPageBase navigatedPage = doc.Pages[i]; string pageNumText = (i + 1).ToString(); SizeF pageNumTextSize = titlesFont.MeasureString(pageNumText); tocPage.Canvas.DrawString(text, titlesFont, PdfBrushes.CadetBlue, 0, y); float dotLocation = titleSize.Width + 2 + x; float pageNumlocation = tocPage.Canvas.ClientSize.Width - pageNumTextSize.Width; for (float j = dotLocation; j < pageNumlocation; j++) { if (dotLocation >= pageNumlocation) { break; } tocPage.Canvas.DrawString(".", titlesFont, PdfBrushes.Gray, dotLocation, y); dotLocation += 3; } tocPage.Canvas.DrawString(pageNumText, titlesFont, PdfBrushes.CadetBlue, pageNumlocation, y); //Add actions that will take you to the target pages when clicked on to the new page location = new PointF(0, y); RectangleF titleBounds = new RectangleF(location, new SizeF(tocPage.Canvas.ClientSize.Width, titleSize.Height)); PdfDestination Dest = new PdfDestination(navigatedPage, new PointF(-doc.PageSettings.Margins.Top, -doc.PageSettings.Margins.Left)); PdfActionAnnotation action = new PdfActionAnnotation(titleBounds, new PdfGoToAction(Dest)); action.Border = new PdfAnnotationBorder(0); (tocPage as PdfNewPage).Annotations.Add(action); y += titleSize.Height + 10; } //Save the result pdf document doc.SaveToFile("AddTableOfContents.pdf"); doc.Close(); } } }[VB.NET]
Imports Spire.Pdf Imports Spire.Pdf.Actions Imports Spire.Pdf.Annotations Imports Spire.Pdf.General Imports Spire.Pdf.Graphics Imports System.Drawing Namespace TableOfContents Friend Class Program Private Shared Sub Main(ByVal args As String()) 'Initialize an instance of the PdfDocument class Dim doc As PdfDocument = New PdfDocument() 'Load a PDF document doc.LoadFromFile("Sample.PDF") 'Get the page count of the document Dim pageCount As Integer = doc.Pages.Count 'Insert a new page into the pdf document as the first page Dim tocPage As PdfPageBase = doc.Pages.Insert(0) 'Draw TOC title on the new page Dim title = "Table of Contents" Dim titleFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial", 20, FontStyle.Bold)) Dim centerAlignment As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle) Dim location As PointF = New PointF(tocPage.Canvas.ClientSize.Width / 2, titleFont.MeasureString(title).Height + 10) tocPage.Canvas.DrawString(title, titleFont, PdfBrushes.CornflowerBlue, location, centerAlignment) 'Draw TOC content on the new page Dim titlesFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial", 14)) Dim titles = New String(pageCount - 1) {} For i = 0 To titles.Length - 1 titles(i) = String.Format("This is page {0}", i + 1) Next Dim y As Single = titleFont.MeasureString(title).Height + 10 Dim x As Single = 0 'Draw page numbers of the target pages on the new page For i = 1 To pageCount Dim text = titles(i - 1) Dim titleSize As SizeF = titlesFont.MeasureString(text) Dim navigatedPage As PdfPageBase = doc.Pages(i) Dim pageNumText As String = (i + 1).ToString() Dim pageNumTextSize As SizeF = titlesFont.MeasureString(pageNumText) tocPage.Canvas.DrawString(text, titlesFont, PdfBrushes.CadetBlue, 0, y) Dim dotLocation = titleSize.Width + 2 + x Dim pageNumlocation As Single = tocPage.Canvas.ClientSize.Width - pageNumTextSize.Width For j = dotLocation To pageNumlocation - 1 If dotLocation >= pageNumlocation Then Exit For End If tocPage.Canvas.DrawString(".", titlesFont, PdfBrushes.Gray, dotLocation, y) dotLocation += 3 Next tocPage.Canvas.DrawString(pageNumText, titlesFont, PdfBrushes.CadetBlue, pageNumlocation, y) 'Add actions that will take you to the target pages when clicked on to the new page location = New PointF(0, y) Dim titleBounds As RectangleF = New RectangleF(location, New SizeF(tocPage.Canvas.ClientSize.Width, titleSize.Height)) Dim Dest As PdfDestination = New PdfDestination(navigatedPage, New PointF(-doc.PageSettings.Margins.Top, -doc.PageSettings.Margins.Left)) Dim action As PdfActionAnnotation = New PdfActionAnnotation(titleBounds, New PdfGoToAction(Dest)) action.Border = New PdfAnnotationBorder(0) TryCast(tocPage, PdfNewPage).Annotations.Add(action) y += titleSize.Height + 10 Next 'Save the result pdf document doc.SaveToFile("AddTableOfContents.pdf") doc.Close() End Sub End Class End Namespace
申请临时许可证
若想从生成的文档中删除评估信息,或解除功能限制,申请 30 天试用许可证