Spire.PDF for .NET【文檔操作】演示:在 PDF 中創建目錄 (TOC)

目錄在增強文檔的可讀性和可導航性方面起著至關重要的作用。它為讀者提供了文檔結構的清晰概述,使他們能夠快速找到并訪問他們感興趣的特定部分或信息。這對于較長的文檔(例如報告、書籍或學術論文)尤其有價值,因為讀者可能需要多次參考特定的部分或章節。在本文中,我們將探討如何使用Spire.PDF for .NET在 C# 和 VB.NET 中在 PDF 文檔中創建目錄。

Spire.PDF for .NET?是一款獨立 PDF 控件,用于 .NET 程序中創建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發人員可以新建一個 PDF 文檔或者對現有的 PDF 文檔進行處理,且無需安裝 Adobe Acrobat。

E-iceblue?功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式(qun:767755948)

Spire.PDF for.net下載? ?Spire.PDF for java下載

安裝 Spire.PDF for .NET

首先,您需要將?Spire.PDF for.NET?包中包含的 DLL 文件作為引用添加到您的 .NET 項目中。

PM> Install-Package Spire.PDF
使用 C# 和 VB.NET 在 PDF 中創建目錄

目錄主要包括目錄標題(例如目錄)、目錄內容、頁碼以及單擊后將帶您進入目標頁面的操作。要使用 Spire.PDF for .NET 在 PDF 中創建目錄,您可以按照以下步驟操作:

  • 初始化PdfDocument類的實例。
  • 使用PdfDocument.LoadFromFile()方法加載 PDF 文檔。
  • 使用PdfDocument.Pages.Count屬性獲取文檔的頁數。
  • 使用PdfDocument.Pages.Insert(0)方法將新頁面插入 PDF 文檔作為第一頁。
  • 使用PdfPageBase.Canvas.DrawString()方法在頁面上繪制目錄標題、目錄內容和頁碼。
  • 使用PdfActionAnnotation類創建操作,并使用PdfNewPage.Annotations.Add()方法將操作添加到頁面。
  • 使用PdfDocument.SaveToFile()方法保存結果文檔。

【C#】

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.DrawingNamespace 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

C#/VB.NET: Create a Table of Contents (TOC) in PDF

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/37709.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/37709.shtml
英文地址,請注明出處:http://en.pswp.cn/web/37709.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

部署calico網絡插件

部署calico網絡插件 之前的k8s環境中主要使用了flannel作為網絡插件&#xff0c;這次改用calico。calico支持多種安裝方式&#xff0c;以下是具體的操作步驟。 1. 準備工作 環境信息 # 系統信息 rootmaster1:~# cat /etc/issue Ubuntu 24.04 LTS \n \lrootmaster1:~# uname…

MyBatisPlus 常用的注解 表映射 主鍵映射 字段映射

介紹 官網&#xff1a;https://baomidou.com/reference/annotation/ 指定映射表 實體類使用駝峰命名&#xff0c;表名應為xx_xxx等格式這樣才可以映射&#xff0c;但是實際開發過程中可能不一致就可以使用該方法處理。 Data TableName("employee_235") //映射的表…

求質數題目

//需求:鍵盤錄入一個正整數x&#xff0c;判斷該整數是否為一個質數。 //質數: //如果一個整數只能被1和本身整除&#xff0c;那么這個數就是質數。否則這個數叫做合數 package Base_se.Base_701;import java.util.Scanner;/*** author gyf* ClassName test* Date 2024/7/1 19:…

Linux啟動elasticsearch,提示權限不夠

Linux啟動elasticsearch&#xff0c;提示權限不夠&#xff0c;如下圖所示&#xff1a; 解決辦法&#xff1a; 設置文件所有者&#xff0c;即使用戶由權限訪問文件 sudo chown -R 用戶名[:新組] ./elasticsearch-8.10.4 //切換到elasticsearch-8.10.4目錄同級 chown詳細格式…

銀行家算法-操作系統中避免死鎖的最著名算法

背景 有很多文章都會介紹銀行家算法。在百度和CSDN上搜一搜能搜出很多來。很多同學會覺得這個算法很深奧&#xff0c;有些文章寫的又很復雜&#xff0c;其實真的很簡單。這里簡單記錄一下基本原理&#xff0c;然后大家再配合其他文章看&#xff0c;就能加深理解。 算法原理 …

LLaVA1.5訓練數據和時間分析

LLaVA的PT+SFT訓練_llava sft-CSDN博客文章瀏覽閱讀379次。這個階段,使用8個A100(80G)訓練LLaVA-v1.5-13B大約需要20h。全量微調,非lora跑不起來啊,以前一直用swift,llama-factory這種框架式的代碼庫,但用原作者開源的代碼也是有很多好處的。在這個階段,使用 8 個 A100(…

Oracle中 ROW_NUMBER()的語法及在對應不同需求下應如何使用

Oracle數據庫中的ROW_NUMBER()函數是一個窗口函數&#xff0c;它為查詢結果集中的每一行分配一個唯一的序號。這個函數在數據分析、分頁查詢、數據去重和排名問題等方面非常有用。ROW_NUMBER()函數的語法如下&#xff1a; ROW_NUMBER() OVER ( [ PARTITION BY column ] ORDER …

3.用戶程序與驅動交互

驅動程序請使用第二章https://blog.csdn.net/chenhequanlalala/article/details/140034424 用戶app與驅動交互最常見的做法是insmod驅動后&#xff0c;生成一個設備節點&#xff0c;app通過open&#xff0c;read等系統調用去操作這個設備節點&#xff0c;這里先用mknode命令調…

64.WEB滲透測試-信息收集- WAF、框架組件識別(4)

免責聲明&#xff1a;內容僅供學習參考&#xff0c;請合法利用知識&#xff0c;禁止進行違法犯罪活動&#xff01; 內容參考于&#xff1a; 易錦網校會員專享課 上一個內容&#xff1a;63.WEB滲透測試-信息收集- WAF、框架組件識別&#xff08;3&#xff09;-CSDN博客 我們在…

【FedMut】Generalized Federated Learning via Stochastic Mutation

基于隨機變異的泛化聯邦學習 來源&#xff1a;AAAI2024 Abstract 問題&#xff1a; FedAvg 將相同的全局模型派發給客戶端進行本地訓練&#xff0c;容易陷入尖銳解&#xff0c;導致訓練出性能低下的全局模型 提出 FedMut&#xff1a; 本文提出了一種名為 FedMut 的新型FL方法…

2024免費的股票數據接口API

滄海數據 # Restful API https://tsanghi.com/api/fin/stock/{exchange_code}/realtime?token5dbb47113a4a43a6be1755673ce854db&ticker{ticker} 數據來源&#xff1a;滄海數據 請求方式&#xff1a;Get 數據格式&#xff1a;標準Json格式[{},...{}]

如何借用物聯網快速實現高標準農田信息化

如何借用物聯網快速實現高標準農田信息化 高標準農田信息化&#xff0c;作為現代農業發展的重要基石&#xff0c;是指在建設高產、穩產、節水、環保的農田基礎上&#xff0c;深度融合現代信息技術&#xff0c;實現農田管理的精準化、智能化和高效化。物聯網&#xff08;Intern…

vue3+ts實現計算兩個字符串的相似度

在TypeScript中&#xff0c;可以使用Levenshtein萊文斯坦距離算法來精確匹配兩個字符串的相似度百分比。Levenshtein距離是指兩個字符串之間&#xff0c;由一個轉換成另一個所需的最少編輯操作次數&#xff0c;這里的編輯操作包括插入、刪除、替換。 /*** Levenshtein距離算法…

Linux Static calls機制

文章目錄 前言一、簡介二、Background: indirect calls, Spectre, and retpolines2.1 Indirect calls2.2 Spectre (v2)2.3 RetpolinesConsequences 2.4 Static callsHow it works 三、其他參考資料 前言 Linux內核5.10內核版本引入新特性&#xff1a;Static calls。 Static c…

JAVA各版本-安裝教程

目錄 Java安裝包下載 Java安裝步驟 Java環境配置 Java安裝包下載 到Oracle官網下載自己需要的版本 Oracle Java下載&#xff1a;Java Archive | Oracle Hong Kong SAR, PRC 下拉選擇自己需要的版本&#xff08;本教程以Windows環境下&#xff0c;JAVA11為例&#xff09; 注…

C++初學者指南-3.自定義類型(第一部分)-指針

C初學者指南-3.自定義類型(第一部分)-指針 文章目錄 C初學者指南-3.自定義類型(第一部分)-指針1.為什么我們需要它們&#xff1f;2.T 類型的對象指針原始指針&#xff1a;T * 智能指針(C11) 3.操作符地址操作符 &解引用運算符 *成員訪問操作符 ->語法重定向 4.nullptr (…

【Linux】用戶管理

創建與刪除 adduser adduser 是一個交互式命令&#xff0c;用于創建新用戶并設置初始環境。 sudo adduser 用戶名示例&#xff1a; sudo adduser newuseruseradd useradd 是一個非交互式命令&#xff0c;允許你通過選項指定用戶的屬性。 sudo useradd [選項] 用戶名常見選…

SCADA系統對于工業生產的意義!

關鍵字:LP-SCADA系統, 傳感器可視化, 設備可視化, 獨立SPC系統, 智能儀表系統,SPC可視化,獨立SPC系統 SCADA系統在智能制造中扮演著至關重要的角色&#xff0c;它通過集成和自動化工廠車間的各種過程&#xff0c;提高了生產效率和產品質量&#xff0c;降低了成本&#xff0c;并…

【AI繪畫 ComfyUI】全新整合包來襲!一鍵安裝 即開即用,超好用的工作流形式的AI繪畫工具!

大家好&#xff0c;我是畫畫的小強 請在看這篇文章的人注意&#xff0c;本文章介紹的Comfy UI整合包是一個節點式的工作&#xff0c;流式的AI繪畫界面&#xff0c;并不適合新手使用。 如果你在找的是Web UI, 請前往我之前發布一篇的文章AI繪畫『Stable Diffusion』面向小白的…

【高中數學/基本不等式】設a,b>0.a+b=5,則 根號下(a+1)+根號下(b+3) 的最大值為?(2015重慶卷)

【問題】 設a,b>0.ab5,則根號下(a1)根號下(b3)的最大值為&#xff1f; 【解答】 解法一&#xff1a; 因雙根號計算不便&#xff0c;故采用平方后簡化之。 原式的平方a12倍根號下((a1)(b3))b3 ab42倍根號下((a1)(b3)) 因為ab5 a1b31359 9(a1)(b3)>2倍根號下((a1)…