title: Search for text in PDF using iText7 and get back entire box text
body: <p>VB2017 using iText7. I am looking for a method to search for key text in a PDF. When I locate the key text I want to return all text within the box that it resides in.</p>
  
  <p>For example, in this PDF I look for the key phrase "usable length" and want to return the text in the box where it was found "Rwy 33 PAPI-L, usable length, notes."</p>
  
  <p><a href="https://i.stack.imgur.com/3aTHz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3aTHz.png" alt="enter image description here"></a></p>
  
  <p>Here is what I have so far (<a href="https://www.programcreek.com/java-api-examples/index.php?api=com.itextpdf.text.pdf.parser.TextExtractionStrategy" rel="nofollow noreferrer">based on this</a>) and would like any suggestions or advice on the concept:</p>
  
  <pre><code>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 &gt; 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 &amp; 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 &gt; 0)
      Debug.Print("match count={0}", linesMatch.Count)
      For Each line In linesMatch
          Debug.Print("line={0}", line)
      Next line
  
      Return linesMatch
  End Function
  </code></pre>
  
  <p>Testing this on a sample PDF results in</p>
  
  <pre><code>FindTextInPdfFile(pdf, "usable length", True)
  match count=1
  line=Rwy 33 PAPI-L, usable length, notes.
  </code></pre>