Ищите текст в PDF с помощью iText7 и возвращайте весь текст в поле - PullRequest
0 голосов
/ 09 июля 2019

VB2017 с использованием iText7.Я ищу метод для поиска ключевого текста в PDF.Когда я нахожу ключевой текст, я хочу вернуть весь текст в поле, в котором он находится.

Например, в этом PDF-документе я ищу ключевую фразу «полезная длина» и хочу вернуть текст вкоробка, в которой он был найден "Rwy 33 PAPI-L, полезная длина, примечания."на основании этого ) и хотели бы получить какие-либо предложения или советы по поводу концепции:

Public Function FindTextInPdfFile(ByVal fileName As String, ByVal searchText As String, ByVal IsCaseSensitive As Boolean) As List(Of String)
    'basic checks
    If String.IsNullOrWhiteSpace(fileName) Then Return Nothing
    If String.IsNullOrWhiteSpace(searchText) Then Return Nothing
    If Not File.Exists(fileName) Then Return Nothing

    'setup the regex to use or not use case sensitivity in the match
    Dim pattern As String = String.Format("({0})", searchText)
    Dim regEx As Regex = Nothing
    If IsCaseSensitive Then
        regEx = New Regex(pattern)
    Else
        regEx = New Regex(pattern, RegexOptions.IgnoreCase)
    End If

    'setup the extraction strategy and temp buffer
    Dim strategy As ITextExtractionStrategy = New SimpleTextExtractionStrategy
    Dim buffBasic As New StringBuilder

    'open the PDF and do a basic search for the text in each page. for each page where we detect the search item
    'we will add that to the temp buffer.
    Using pdfReader As PdfReader = New PdfReader(fileName)
        Using pdfDocument As PdfDocument = New PdfDocument(pdfReader)
            For pageNum As Integer = 1 To pdfDocument.GetNumberOfPages
                Dim page As PdfPage = pdfDocument.GetPage(pageNum)
                Dim currentPageText As String = PdfTextExtractor.GetTextFromPage(page, strategy)

                If regEx.Matches(currentPageText).Count > 0 Then
                    'Debug.Print("found search text [{0}] in page num {1}", searchText, pageNum)
                    'Debug.Print("GetResultantText={0}", strategy.GetResultantText)

                    'GetResultantText has lines of text separated by an LF
                    buffBasic.Append(strategy.GetResultantText & lf)
                End If
            Next pageNum
        End Using
    End Using

    'the buffer should have lines of text separated by an LF
    Dim linesBasic As List(Of String) = buffBasic.ToString.Split(lf).ToList
    Dim linesMatch As List(Of String) = linesBasic.FindAll(Function(x) regEx.Matches(x).Count > 0)
    Debug.Print("match count={0}", linesMatch.Count)
    For Each line In linesMatch
        Debug.Print("line={0}", line)
    Next line

    Return linesMatch
End Function

Проверка этого на примере PDF приведет к

FindTextInPdfFile(pdf, "usable length", True)
match count=1
line=Rwy 33 PAPI-L, usable length, notes.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...