使用.NET版本的PDFBox解析来自PDF的注释/注释:PDFBox.NET-1.8.9

时间:2018-09-03 11:27:24

标签: parsing text pdfbox

我正在使用以下代码使用.NET版本的PDFBox解析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类提取它们,但希望坚持使用PDFBox。

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

1 个答案:

答案 0 :(得分:2)

我通过“评论”假设您是指具有名称评论文本注释。以下代码输出所有文本注释的内容。如果您要使用其他注释类型,则可能需要对其进行调整:

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