没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|行业资讯|编辑:胡涛|2024-06-28 09:38:28.390|阅读 20 次
概述:在本文中,我们将探讨如何使用Spire.PDF for .NET在 C# 和 VB.NET 中在 PDF 文档中创建目录。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
目录在增强文档的可读性和可导航性方面起着至关重要的作用。它为读者提供了文档结构的清晰概述,使他们能够快速找到并访问他们感兴趣的特定部分或信息。这对于较长的文档(例如报告、书籍或学术论文)尤其有价值,因为读者可能需要多次参考特定的部分或章节。在本文中,我们将探讨如何使用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 等格式
Spire.PDF for.net下载 Spire.PDF for java下载
首先,您需要将 Spire.PDF for.NET 包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。
PM> Install-Package Spire.PDF
目录主要包括目录标题(例如目录)、目录内容、页码以及单击后将带您进入目标页面的操作。要使用 Spire.PDF for .NET 在 PDF 中创建目录,您可以按照以下步骤操作:
【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.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
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询慧都在线客服 ;技术交流Q群(767755948)
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
Parasoft dotTEST凭借其即插即用的内置安全标准和直观的规则映射能力,为C#/VB.NET 开发者提供了强大的支持。它有效消除了理解复杂标准与配置规则的障碍,让开发者能够专注于编写高质量代码,而非耗费精力在标准解读与工具映射上。这不仅显著提升了开发效率,缩短了交付周期,更从源头系统性地降低了安全风险,确保了软件符合严苛的行业合规要求。
Parasoft Virtualize以智能复用和轻量存储为核心,不仅解决了传统测试数据管理的冗余与性能瓶颈,更通过技术创新释放了团队的生产力。它让开发者摆脱重复劳动的束缚,将精力聚焦于更复杂的业务逻辑与质量验证,从而在快节奏的开发周期中保持敏捷与精准。无论是应对高并发的性能挑战,还是满足数据合规的严苛要求,Virtualize都以优秀的解决方案为技术团队提供了坚实的支撑。
imScale近期与 西门子PLM软件公司 和Tech Soft 3D达成技术合作,将行业领先的几何建模内核Parasolid及多格式CAD数据解析引擎HOOPS Exchange深度集成至平台中,全面增强CAD数据处理与互操作能力。
今天为大家介绍Kotlin 开发者如何在 Android Studio 中使用 JetBrains AI Assistant ,欢迎下载工具体验!
Spire.PDF for .NET是独立的PDF控件,用于.NET程序中创建、编辑和操作PDF文档
wPDF为Delphi 和C++Builder应用程序快速而方便的创建PDF文件的支持。
Aspose.PDF for .NETPDF文档创建组件,无需Adobe Acrobat,也可以在任何平台上操作PDF文档。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号