title: Using pdftools in R to extract specific table after a string
body: <p>I have couple of pdfs and I wish to extract the shareholders table. How can I specify such that only table appearing after the string 'TWENTY LARGEST SHAREHOLDERS' is extracted?</p>
  <p>I tried but was not quite sure of the function part.</p>
  <pre><code>library(&quot;pdftools&quot;)
  library(&quot;tidyverse&quot;)
  
  url &lt;- c(&quot;https://www.computershare.com/News/Annual%20Report%202019.pdf?2&quot;)
  
  raw_text &lt;- map(url, pdf_text)
  
  
  clean_table &lt;- function(table){
    table &lt;- str_split(table, &quot;\n&quot;, simplify = TRUE)
    table_start &lt;- stringr::str_which(table, &quot;TWENTY LARGEST SHAREHOLDERS&quot;)
    table &lt;- table[1, (table_start +1 ):(table_end - 1)]
    table &lt;- str_replace_all(table, &quot;\\s{2,}&quot;, &quot;|&quot;)
    text_con &lt;- textConnection(table)
    data_table &lt;- read.csv(text_con, sep = &quot;|&quot;)
    colnames(data_table) &lt;- c(&quot;Name&quot;, &quot;Number of Shares&quot;, &quot;Percentage&quot;)
  }
  
  shares &lt;- map_df(raw_text, clean_table) 
  
  </code></pre>