【问题标题】:Parse Comments / Annotations From PDF Using .NET Version of PDFBox: PDFBox.NET-1.8.9使用 PDFBox 的 .NET 版本解析 PDF 中的注释/注释:PDFBox.NET-1.8.9
【发布时间】:2018-09-03 11:27:24
【问题描述】:

我正在使用以下代码使用 .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

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

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

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

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

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

【问题讨论】:

  • 使用 PDFBox 也很简单。您使用 VB,但在任何地方都没有提到确切的 .Net 语言。这是否意味着 C# 代码也可以?
  • 谢谢@mkl 我已修改问题以包含 .NET 版本并补充说,虽然我的首选语言是 VB,但我也很高兴收到 C# 的答案。
  • 嗨@mkl 我知道你说“使用 PDFBox 做这件事很简单”。我已经进一步搜索,但仍然是空白。我在这里寻找灵感:pdfbox.apache.org/docs/2.0.5/javadocs/index.html?org/apache/…

标签: parsing text pdfbox


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多