在 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 天試用許可證 

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線