title: Extract Pages from a PDF using itextsharp in Powershell
body: <p>I have been researching this for weeks now and can't seem to make much ground on the subject. I have a large PDF (900+ pages), that is the result of a mail merge. The result is 900+ copies of the same document which is one page, with the only difference being someone's name on the bottom. What I am trying to do, is have a powershell script read the document using itextsharp and save pages that contain a specific string (the person's name) into their respective folder. </p>
  
  <p>This is what I have managed so far.</p>
  
  <pre><code>Add-Type -Path C:\scripts\itextsharp.dll
  
  $reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList 
  "$pwd\downloads\TMs.pdf"
  for($page = 1; $page -le $reader.NumberOfPages; $page++) {
  
  
      $pageText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader,$page).Split([char]0x000A)
  
      if($PageText -match 'DAN KAGAN'){
      Write-Host "DAN FOUND"
      }
      }
  </code></pre>
  
  <p>As you can see I am only using one name for now for testing. The script finds the name properly 10 times. What I cannot seem to find any information on, is how to extract pages that this string appears on.</p>
  
  <p>I hope this was clear. If I can be of any help, please let me know.</p>
  
  <p>Thanks!</p>