在 PDF 中創(chuàng)建目錄 (TOC)
Spire.PDF for .NET 是一款專門(mén)對(duì) Word 文檔進(jìn)行操作的 .NET 類(lèi)庫(kù)。致力于在于幫助開(kāi)發(fā)人員輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔,而無(wú)需安裝 Microsoft Word。
行號(hào)用于在每行文本旁邊顯示 Word 自動(dòng)計(jì)算的行數(shù)。當(dāng)我們需要參考合同或法律文件等文檔中的特定行時(shí),它非常有用。word中的行號(hào)功能允許我們?cè)O(shè)置起始值、編號(hào)間隔、與文本的距離以及行號(hào)的編號(hào)方式。使用 Spire.Doc,我們可以實(shí)現(xiàn)上述所有功能。本文將介紹如何將 HTML 轉(zhuǎn)換為 PDF。
歡迎加入spire技術(shù)交流群:767755948
目錄在提高文件的可讀性和可瀏覽性方面起著至關(guān)重要的作用。它為讀者提供了文檔結(jié)構(gòu)的清晰概覽,使他們能夠快速查找和訪問(wèn)感興趣的特定章節(jié)或信息。這對(duì)于報(bào)告、書(shū)籍或?qū)W術(shù)論文等篇幅較長(zhǎng)的文檔尤為重要,因?yàn)樽x者可能需要多次查閱特定章節(jié)。在本文中,我們將探討如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中創(chuàng)建 PDF 文檔的目錄。
安裝 Spire.PDF for .NET
首先,您需要將 Spire.PDF for.NET 軟件包中包含的 DLL 文件作為引用添加到您的 .NET 項(xiàng)目中。DLL 文件既可以從這個(gè)鏈接下載,也可以通過(guò) NuGet 安裝。
PM> Install-Package Spire.PDF在 C# 和 VB.NET 中創(chuàng)建 PDF 目錄
目錄主要包括 TOC 標(biāo)題(如目錄)、TOC 內(nèi)容、頁(yè)碼以及點(diǎn)擊后可進(jìn)入目標(biāo)頁(yè)面的操作。使用 Spire.PDF for .NET 在 PDF 中創(chuàng)建目錄的步驟如下:
- 初始化 PdfDocument 類(lèi)的實(shí)例。
- 使用 PdfDocument.LoadFromFile() 方法加載 PDF 文檔。
- 使用PdfDocument.Pages.Count屬性獲取文檔頁(yè)數(shù)。
- 使用PdfDocument.Pages.Insert(0)方法在PDF文檔中插入新頁(yè)作為第一頁(yè)。
- 使用PdfPageBase.Canvas.DrawString()方法在頁(yè)面上繪制TOC標(biāo)題、TOC內(nèi)容和頁(yè)碼。
- 使用PdfActionAnnotation類(lèi)創(chuàng)建動(dòng)作,并使用PdfNewPage.Annotations.Add()方法將動(dòng)作添加到頁(yè)面中。
- 使用PdfDocument.SaveToFile()方法保存結(jié)果文檔。
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
申請(qǐng)臨時(shí)許可證
若想從生成的文檔中刪除評(píng)估信息,或解除功能限制,申請(qǐng) 30 天試用許可證