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.mzph.cn/web/37709.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux启动elasticsearch,提示权限不够

Linux启动elasticsearch&#xff0c;提示权限不够&#xff0c;如下图所示&#xff1a; 解决办法&#xff1a; 设置文件所有者&#xff0c;即使用户由权限访问文件 sudo chown -R 用户名[:新组] ./elasticsearch-8.10.4 //切换到elasticsearch-8.10.4目录同级 chown详细格式…

LLaVA1.5训练数据和时间分析

LLaVA的PT+SFT训练_llava sft-CSDN博客文章浏览阅读379次。这个阶段,使用8个A100(80G)训练LLaVA-v1.5-13B大约需要20h。全量微调,非lora跑不起来啊,以前一直用swift,llama-factory这种框架式的代码库,但用原作者开源的代码也是有很多好处的。在这个阶段,使用 8 个 A100(…

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…

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 (…

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)…

【小贪】项目实战——Zero-shot根据文字提示分割出图片目标掩码

目标描述 给定RGB视频或图片&#xff0c;目标是分割出图像中的指定目标掩码。我们需要复现两个Zero-shot的开源项目&#xff0c;分别为IDEA研究院的GroundingDINO和Facebook的SAM。首先使用目标检测方法GroundingDINO&#xff0c;输入想检测目标的文字提示&#xff0c;可以获得…

uniapp中如何进行微信小程序的分包

思路&#xff1a;在uniapp中对微信小程序进行分包&#xff0c;和原生微信小程序进行分包的操作基本上没区别&#xff0c;主要就是在pages.json中进行配置。 如图&#xff0c;我新增了一个包diver-page 此时需要在pages.json中的subPackages数组中新增一项 root代表这个包的根…

用好华为小助手,生活总能快人一步

嘿&#xff01;朋友们&#xff01;你们有没有想过&#xff0c;如果身边有一个小助手&#xff0c;他不仅聪明伶俐&#xff0c;还能在生活的方方面面给予你最贴心的关怀和帮助&#xff0c;让我们的日常生活变得更加方便和快捷&#xff0c;那该有多好&#xff01;没错&#xff0c;…

谈谈创意设计中的AI、AGI、AIGC

在当今的数字化时代&#xff0c;创意设计领域正经历着前所未有的变革。随着人工智能&#xff08;AI&#xff09;、通用人工智能&#xff08;AGI&#xff09;以及人工智能生成内容&#xff08;AIGC&#xff09;的迅猛发展&#xff0c;设计师们的工作方式和创作手段都发生了深刻的…

Swift 中的 StoreKit 测试

文章目录 前言创建一个 StoreKit Demo使用 SKTestSessionaskToBuyEnabled 属性总结前言 StoreKit 框架的第二次迭代是我在过去几年中应用程序中最重大的变化。最近版本的 StoreKit 框架已完全采用了 Swift 语言特性,如 async 和 await。本篇内容我们将讨论 StoreKitTest 框架…

【揭秘】嘴尚绝卤味健康新风尚,让你吃得美味又健康!

在快节奏的现代生活中&#xff0c;美食不仅是味蕾的享受&#xff0c;更是健康生活的追求。今天&#xff0c;我们要聊的就是备受食客们青睐的“嘴尚绝卤味”——如何在享受美味的同时&#xff0c;也能兼顾健康饮食的理念。 一、卤味文化&#xff0c;源远流长 卤味&#xff0c;作…

Redis缓存管理机制

在当今快节奏的数字世界中&#xff0c;性能优化对于提供无缝的用户体验至关重要。缓存在提高应用程序性能方面发挥着至关重要的作用&#xff0c;它通过将经常使用或处理的数据存储在临时高速存储中来减少数据库负载并缩短响应时间&#xff0c;从而减少系统的延迟。Redis 是一种…

navicat Lite 版

navicat Lite 版&#xff1a; Navicat 出了一个 Navicat Premium 的Lite版。 官方现在链接&#xff1a;https://www.navicat.com.cn/download/navicat-premium-lite#windows 从官网可以看到现在能够下载最新版本 17&#xff0c;支持各种平台