我正在使用以下代码使用.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#的答案。
我假设“注释”是指具有Name值Comment的文本注释。以下代码输出所有文本注释的内容。如果您要使用其他注释类型,则可能需要对其进行调整:
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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句