title: remove invisible text from pdf using pdfbox
body: <p><a href="https://drive.google.com/file/d/1F8vrzcABwxVGdN5W-7etQggY5xKtGplU/view" rel="nofollow noreferrer">Link to pdf</a></p>
  
  <p>When I try to extract the text from the pdf above, I get a mixture of text that was invisible in the evince viewer as well as text that was visible. In addition, some of the desired text is missing characters that were not missing in the viewer, such as, the 'S' in 'FALCONS' and the many missing '½' characters. I believe this is due to interference from the invisible text because when highlighting the pdf in the viewer, the invisible text can be seen overlapping visible text.</p>
  
  <p>Is there a way to remove the invisible text? Or is there another solution?</p>
  
  <p>Code:</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.text.PDFTextStripper;
  
  
  public class App {
  
      public static String getPdfText(String pdfPath) throws IOException {
          File file = new File(pdfPath);
          PDDocument document = null;
          PDFTextStripper textStripper = null;
          String text = null;
  
          try {
              document = PDDocument.load(file);
              textStripper = new PDFTextStripper();
              textStripper.setEndPage(1);
              text =  textStripper.getText(document);
          } catch (IOException e) {
              throw new IOException("Could not load file and strip text.", e);
          } finally {
              try {
                  if (document != null)
                      document.close();
              } catch (IOException e) {
                  System.out.println("Could not close document");
              }
          }
  
          return text;
      }
  
      public static void main(String[] args) {
          String filename = "RevTeaser09072016.pdf";
          String text = null;
  
          try {
              text = getPdfText(filename);
          } catch (IOException e) {
              e.printStackTrace();
              System.exit(1);
          }
  
          System.out.println(text);
      }
  }
  </code></pre>
  
  <p>Output (bold text is the desired text):</p>
  
  <pre>
  145
  143
  159
  144
  160
  141
  157155 156154150 153149 152148 151147
  142
  158
  500
  146
  Selections
  Number of Teams
  Amount Bet
  REVERSE tEaSER caRd
  mark box as shown 
   denotes home team
  PRO FOOTBALL - THURSDAY,  NOVEMBER 15, 2012
  1 BILLS ★ NFL  PM8:25 2 DOLPHINS7– ½ 6– ½
  PRO FOOTBALL - SUNDAY, NOVEMBER 18, 2012
  3 REDSKINS ★  PM1:00 4 EAGLES10– ½ 3– ½
  5 PACKERS  PM1:00 6 LIONS ★10– ½ 3– ½
  7 FALCONS ★  PM1:00 8 CARDINALS17– ½ 3+ ½
  9 BUCCANEERS  PM1:00 10 PANTHERS ★7– ½ 6– ½
  11 COWBOYS ★  PM1:00 12 BROWNS14– ½ + ½
  13 RAMS ★  PM1:00 14 JETS10– ½ 3– ½
  15 PATRIOTS ★  PM4:25 16 COLTS17– ½ 3+ ½
  17 TEXANS ★  PM1:00 18 JAGUARS23– ½ 9+ ½
  19 BENGALS  PM1:00 20 CHIEFS ★10– ½ 3– ½
  21 SAINTS  PM4:05 22 RAIDERS ★12– ½ 1– ½
  23 BRONCOS ★  PM4:25 24 CHARGERS14– ½ + ½
  25 RAVENS NBC  PM8:30 26 STEELERS ★7– ½ 6– ½
  PRO FOOTBALL - MONDAY, NOVEMBER 19, 2012
  27 49ERS ★ ESPN  PM8:40 28 BEARS10– ½ 3– ½
  1,000
  145
  143
  159
  144
  160
  141
  157155 156154150 153149 152148 151147
  142
  158
  500
  146
  Selections
  Number of Teams
  Amount Bet
  REVERSE tEaSER caRd
  mark box as hown 
   denotes home team
  PRO FOOTBALL - THURSDAY,  NOVEMBER 15, 2012
  1 BILLS ★ NFL  PM8:25 2 DOLPHINS7– ½ 6– ½
  PRO FOOTBALL - SUNDAY, NOVEMBER 18, 2012
  3 REDSKINS ★  PM1:00 4 EAGLES10– ½ 3– ½
  5 PACKERS  PM1:00 6 LIONS ★10– ½ 3– ½
  7 FALCONS ★  PM1:00 8 CARDINALS17– ½ 3+ ½
  9 BUCCANEERS  PM1:00 10 PANTHERS ★7– ½ 6– ½
  11 COWBOYS ★  PM1:00 12 BROWNS14– ½ + ½
  13 RAMS ★  PM1:00 14 JETS10– ½ 3– ½
  15 PATRIOTS ★  PM4:25 16 COLTS17– ½ 3+ ½
  17 TEXANS ★  PM1:00 18 JAGUARS23– ½ 9+ ½
  19 BENGALS  PM1:00 20 CHIEFS ★10– ½ 3– ½
  21 SAINTS  PM4:05 22 RAIDERS ★12– ½ 1– ½
  23 BRONCOS ★  PM4:25 24 CHARGERS14– ½ + ½
  25 RAVENS NBC  PM8:30 26 STEEL RS ★7– ½ 6– ½
  PRO FOOTBALL - MONDAY, NOVEMBER 19, 2012
  27 49ERS ★ ESPN  PM8:40 28 BEARS10– ½ 3– ½
  1,000
  145
  143
  159
  14
  160
  41
  15715 156154150 153149 152148 51147
  142
  158
  50
  146
  S lections
  Number of Teams
  Amount Bet
  
  ark box as sho n 
   denotes home team
  PRO F OTBALL - THURSDAY, NOVEMBER 15, 2012
  1 BILLS ★ NFL  PM8:25 2 DOLPHINS7– ½ 6– ½
  PRO F OTBALL - SUNDAY, NOVEMBER 18, 2012
  3 REDSKINS ★  PM1:0 4 EAGLES10– ½ 3– ½
  5 PACKERS  PM1:0 6 LIONS ★10– ½ 3– ½
  7 FALCONS ★  PM1:0 8 CARDINALS17– ½ 3+ ½
  9 BU CANEERS  PM1:0 10 PANTHERS ★7– ½ 6– ½
  11 COWBOYS ★  PM1:0 12 BROWNS14– ½ + ½
  13 RAMS ★  PM1:0 14 JETS10– ½ 3– ½
  15 PATRIOTS ★  PM4:25 16 COLTS17– ½ 3+ ½
  17 TEXANS ★  PM1:0 18 JAGUARS23– ½ 9+ ½
  19 BENGALS  PM1:0 20 CHIEFS ★10– ½ 3– ½
  21 SAINTS  PM4:05 22 RAIDERS ★12– ½ 1– ½
  23 BRONCOS ★  PM4:25 24 CHARGERS14– ½ + ½
  25 RAVENS NBC  PM8:30 26 STEELERS ★7– ½ 6– ½
  PRO F OTBALL - MONDAY, NOVEMBER 19, 2012
  27 49ERS ★ ESPN  PM8:40 28 BEARS10– ½ 3– ½
  1,0
  MARK BOX AS SHOWN 
  DENOTES HOME TEAM
  PRO FOOTBALL - THURSDAY, SEPTEMBER 8, 2016
   <b>1 PANTHERS    nbc  - 10½ 8:30p 2 BRONCOS   - 3½
   PRO FOOTBALL - SUNDAY, SEPTEMBER 11, 2016
    FALCON      - 9  1:00p 4 BUCCANEERS  - 4½
   5 VIKINGS   - 9½ 1:00p 6 TITANS  - 4½
   7 EAGLES  - 10½ 1:00p 8 BROWNS  - 3½
   9 BENGALS - 9½ 1:00p 10 JETS  - 4½
   11 SAINTS    - 7½ 1:00p 12 RAIDERS   - 6½
   13 CHIEFS  - 14½ 1:00p 14 CHARGERS  + ½
   15 RAVENS  - 10½ 1:00p 16 BILLS - 3½
   17 TEXANS  - 14  1:00p 18 BEARS + ½
   19 PACKERS - 12  1:00p 20 JAGUARS  - 1½
   21 SEAHAWKS    - 17½ 4:05p 22 DOLPHINS + 3½
   23 COWBOYS    - 7½ 4:25p 24 GIANTS - 6½
   25 COLTS     - 10½ 4:25p 26 LIONS - 3½
   27 CARDINALS   nbc  - 14½ 8:30p 28 PATRIOTS + ½
   PRO FOOTBALL - MONDAY, SEPTEMBER 12, 2016
   29 STEELERS  espn  - 10½ 7:10p 30 REDSKINS  - 3½
   31 RAMS  espn  - 9  10:20p 32 49ERS  - 4½</b>
  </pre>