使用 PDFBox 的 .NET 版本从 PDF 解析评论/注释:PDFBox.NET-1.8.9

Parse Comments / Annotations From PDF Using .NET Version of PDFBox: PDFBox.NET-1.8.9

我正在使用以下代码使用 PDFBox 的 .NET 版本解析 PDF 中的文本。

Imports org.apache.pdfbox.pdmodel
Imports org.apache.pdfbox.util

Private Shared Function parseUsingPDFBox(ByVal input As String) As String
      Dim doc As PDDocument = Nothing

      Try
        doc = PDDocument.load(input)
        Dim stripper As New PDFTextStripper()
        Return stripper.getText(doc)
      Finally
        If doc IsNot Nothing Then
          doc.close()
        End If
      End Try
    End Function

http://www.squarepdf.net/how-to-convert-pdf-to-text-in-net-vb

代码正在提取纯可见文本,但未提取评论。

我试过使用 FDFAnnotation.ToString() 但它警告说 ToString() 不明确...

doc = PDDocument.load(strFilename)
Dim stripper As New FDFAnnotationText
Return stripper.tostring(doc)

我已经尝试过 iTextSharp,我可以使用 PdfName.ANNOTS class 提取它们,但我希望坚持使用 PDFBox。

我的首选语言是 VB,但我也很乐意接受 C# 的答案。

我假设 "comments" 你的意思是 文本注释名称评论.以下代码输出所有文本注释的Contents。如果您指的是不同的注释类型,您可能需要调整它:

Dim doc As PDDocument = PDDocument.loadNonSeq(New java.io.File("..."), Nothing)
Dim pages As java.util.List = doc.getDocumentCatalog().getAllPages()
For i = 0 To pages.size() - 1
    Dim page As PDPage = pages.get(i)
    Dim annotations As java.util.List = page.getAnnotations()
    For j = 0 To annotations.size() - 1
        Dim annotation As PDAnnotation = annotations.get(j)
        If annotation.getSubtype() = "Text" Then
            Console.WriteLine("{0}-{1} : {2}", i, j, annotation.getContents())
        End If
    Next
Next

doc.close()