使用C#代碼在PDF中創(chuàng)建目錄的方法示例
目錄在提升文檔的可讀性和可導航性方面起著至關重要的作用。它為讀者提供了文檔結(jié)構(gòu)的清晰概覽,使他們能夠快速定位并訪問感興趣的特定章節(jié)或信息。對于較長的文檔(例如報告、書籍或?qū)W術論文)而言,這一點尤為重要,因為讀者可能需要多次返回特定的章節(jié)或部分進行查閱。
本文將介紹如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中創(chuàng)建 PDF 文檔的目錄。
安裝 Spire.PDF for .NET
首先,需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項目的引用。
這些 DLL 文件可以通過以下鏈接下載,或通過 NuGet 安裝。
PM> Install-Package Spire.PDF
在 C# 和 VB.NET 中創(chuàng)建 PDF 目錄
目錄通常包括目錄標題(例如 “Table of Contents”)、目錄內(nèi)容、頁碼,以及可點擊跳轉(zhuǎn)到對應頁面的交互動作。
使用 Spire.PDF for .NET 創(chuàng)建 PDF 目錄時,可以按照以下步驟進行:
- 初始化 PdfDocument 類的實例。
- 使用
PdfDocument.LoadFromFile()方法加載 PDF 文檔。 - 通過
PdfDocument.Pages.Count屬性獲取文檔的頁數(shù)。 - 使用
PdfDocument.Pages.Insert(0)方法在文檔開頭插入一個新頁面作為目錄頁。 - 使用
PdfPageBase.Canvas.DrawString()方法在該頁面上繪制目錄標題、目錄內(nèi)容和頁碼。 - 通過
PdfActionAnnotation類創(chuàng)建跳轉(zhuǎn)動作,并使用PdfNewPage.Annotations.Add()方法將這些動作添加到頁面中。 - 使用
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)
{
//初始化 PdfDocument 類的實例
PdfDocument doc = new PdfDocument();
//加載 PDF 文檔
doc.LoadFromFile("Sample.PDF");
//獲取文檔的頁數(shù)
int pageCount = doc.Pages.Count;
//在文檔開頭插入一個新頁面作為目錄頁
PdfPageBase tocPage = doc.Pages.Insert(0);
//在新頁面上繪制目錄標題
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);
//在新頁面上繪制目錄內(nèi)容
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;
//在新頁面上繪制目標頁的頁碼
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);
//為目錄頁中的文本添加可點擊的跳轉(zhuǎn)動作
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;
}
//保存生成的 PDF 文檔
doc.SaveToFile("AddTableOfContents.pdf");
doc.Close();
}
}
}到此這篇關于使用C#代碼在PDF中創(chuàng)建目錄的方法示例的文章就介紹到這了,更多相關C# PDF創(chuàng)建目錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
提高C# StringBuilder操作性能優(yōu)化的方法
本篇文章主要介紹使用C# StringBuilder 的項目實踐,用于減少內(nèi)存分配,提高字符串操作的性能。對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-11-11
ItemsControl 數(shù)據(jù)綁定的兩種方式
這篇文章主要介紹了ItemsControl 數(shù)據(jù)綁定的兩種方式,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下2021-03-03
C# 并發(fā)控制框架之單線程環(huán)境下實現(xiàn)每秒百萬級調(diào)度
本文介紹了一款專為工業(yè)自動化及機器視覺開發(fā)的C#并發(fā)流程控制框架,通過模仿Go語言并發(fā)模式設計,支持高頻調(diào)度及復雜任務處理,已在多個項目中驗證其穩(wěn)定性和可靠性2024-10-10
在c#中使用servicestackredis操作redis的實例代碼
本篇文章主要介紹了在c#中使用servicestackredis操作redis的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06

