C# 获取PDF中的附件所在页面位置

2024-11-13 07:13:59

本次经验内容以C#代码示例介绍如何来获取PDF文档中附件所在页面位置。

工具/原料

Visual Studio 2017

Net Framework 4.6.1

Spire.PDF for .NET 7.12.1

PDF文档

dll引用

1、在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”-“管理NuGet包”:

C# 获取PDF中的附件所在页面位置

2、选择“浏览”-在搜索框中输入搜索内容,点击搜索到结果,点击“安装”:

C# 获取PDF中的附件所在页面位置

3、在弹出的界面中,选择“OK”,等待程序安装:

C# 获取PDF中的附件所在页面位置

4、安装结果如图效果:

C# 获取PDF中的附件所在页面位置

C#代码

1、 PdfDocument doc = new PdfDocument(); doc.LoadFromFile(input); int pageIndex = GetAttachmentPage(doc.Pages, attachmentName); private int GetAttachmentPage(Spire.Pdf.Widget.PdfPageCollection pageCollection, string fileName) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("fileName is null or empty"); } for (int i = 0; i < pageCollection.Count; i++) { PdfPageBase page = pageCollection[i]; for (int j = 0; j < page.AnnotationsWidget.Count; j++) { Spire.Pdf.Annotations.PdfFileLinkAnnotationWidget pdfFileLinkAnnotationWidget = page.AnnotationsWidget[j] as Spire.Pdf.Annotations.PdfFileLinkAnnotationWidget; if (pdfFileLinkAnnotationWidget != null) { if (fileName.Equals(pdfFileLinkAnnotationWidget.FileName)) { return i; } } } } return -1; }

猜你喜欢