You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用VB.Net仅导出PDF中的批注

在VB.NET中提取PDF批注并导出到工作表的可行方案

核心思路

借助成熟的.NET PDF处理库提取批注数据,再将数据导出到Excel工作表。推荐使用iText 7(或旧版iTextSharp)、PdfSharp这两个库,它们都能直接读取PDF中的批注信息。


方案一:使用iText 7提取批注并导出到Excel

iText是功能完善的PDF处理库,支持读取各类批注、处理加密PDF,适合复杂场景。

  1. 安装依赖
    在Visual Studio的NuGet包管理器中安装:
  • iText7(核心PDF处理库)
  • EPPlus(用于导出Excel工作表)
  1. VB.NET代码实现
Imports iText.Kernel.Pdf
Imports iText.Kernel.Pdf.Annot
Imports System.Data
Imports OfficeOpenXml
Imports System.IO

' 提取PDF批注到DataTable
Private Function ExtractPdfAnnotations(pdfPath As String) As DataTable
    Dim annotationTable As New DataTable()
    ' 定义工作表列,可根据需求调整
    annotationTable.Columns.Add("批注类型", GetType(String))
    annotationTable.Columns.Add("批注内容", GetType(String))
    annotationTable.Columns.Add("所在页码", GetType(Integer))
    annotationTable.Columns.Add("作者", GetType(String))
    annotationTable.Columns.Add("创建时间", GetType(DateTime))

    ' 处理加密PDF可传入密码:New PdfReader(pdfPath, New ReaderProperties().SetPassword("密码".ToCharArray()))
    Using pdfReader As New PdfReader(pdfPath)
        Using pdfDoc As New PdfDocument(pdfReader)
            For pageIndex As Integer = 1 To pdfDoc.GetNumberOfPages()
                Dim currentPage As PdfPage = pdfDoc.GetPage(pageIndex)
                Dim pageAnnotations = currentPage.GetAnnotations()

                For Each annot As PdfAnnotation In pageAnnotations
                    ' 过滤表单控件类批注,仅保留普通批注
                    If Not TypeOf annot Is PdfWidgetAnnotation Then
                        Dim newRow As DataRow = annotationTable.NewRow()
                        newRow("批注类型") = annot.GetSubtype().ToString()
                        newRow("批注内容") = annot.GetContents()
                        newRow("所在页码") = pageIndex
                        newRow("作者") = annot.GetAuthor()
                        newRow("创建时间") = annot.GetCreationDate().GetValue()
                        annotationTable.Rows.Add(newRow)
                    End If
                Next
            Next
        End Using
    End Using
    Return annotationTable
End Function

' 将批注数据导出到Excel
Private Sub ExportAnnotationsToExcel(annotationsData As DataTable, excelOutputPath As String)
    If File.Exists(excelOutputPath) Then File.Delete(excelOutputPath)

    Using excelPackage As New ExcelPackage()
        Dim worksheet = excelPackage.Workbook.Worksheets.Add("PDF批注")
        ' 写入表头
        For col As Integer = 0 To annotationsData.Columns.Count - 1
            worksheet.Cells(1, col + 1).Value = annotationsData.Columns(col).ColumnName
            ' 设置表头样式(可选)
            worksheet.Cells(1, col + 1).Style.Font.Bold = True
        Next
        ' 批量写入数据
        worksheet.Cells(2, 1).LoadFromDataTable(annotationsData, False)
        ' 自动调整列宽
        worksheet.Cells.AutoFitColumns()

        excelPackage.SaveAs(New FileInfo(excelOutputPath))
    End Using
End Sub

' 调用示例
Private Sub ProcessPdfAnnotations()
    Dim sourcePdfPath = "C:\YourSourceFile.pdf"
    Dim targetExcelPath = "C:\Annotations_Export.xlsx"

    Dim annotations = ExtractPdfAnnotations(sourcePdfPath)
    ExportAnnotationsToExcel(annotations, targetExcelPath)
End Sub

方案二:使用PdfSharp快速提取批注

PdfSharp是轻量级PDF库,API更简洁,适合简单场景。

  1. 安装依赖
    NuGet安装PdfSharp和EPPlus。

  2. VB.NET代码实现

Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.Annotations
Imports System.Data
Imports OfficeOpenXml
Imports System.IO

Private Function ExtractAnnotationsWithPdfSharp(pdfPath As String) As DataTable
    Dim annotationTable As New DataTable()
    annotationTable.Columns.Add("批注类型", GetType(String))
    annotationTable.Columns.Add("批注内容", GetType(String))
    annotationTable.Columns.Add("所在页码", GetType(Integer))

    Dim pdfDoc As PdfDocument = PdfReader.Open(pdfPath, PdfDocumentOpenMode.Import)
    For Each page As PdfPage In pdfDoc.Pages
        For Each annot As PdfAnnotation In page.Annotations
            Dim newRow As DataRow = annotationTable.NewRow()
            newRow("批注类型") = annot.AnnotationType.ToString()
            newRow("批注内容") = annot.Contents
            newRow("所在页码") = pdfDoc.Pages.IndexOf(page) + 1
            annotationTable.Rows.Add(newRow)
        Next
    Next
    Return annotationTable
End Function

' 导出Excel的函数可复用方案一中的ExportAnnotationsToExcel

额外说明

  • 如果需要导出类似FDF的格式:FDF是XML结构的批注文件,你可以将提取到的DataTable序列化为符合FDF规范的XML(核心节点为/FDF/Annots),直接保存为.fdf文件即可。
  • 加密PDF处理:两个库都支持传入密码解密,参考代码中的注释。
  • 批注类型过滤:可根据需求筛选特定类型的批注(比如高亮Highlight、文本注释Text等)。

内容的提问来源于stack exchange,提问作者Manjunath M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 18:51:22