title: How to extract content from a pdf having both text and images?
body: <p>I have a pdf file which has two types of pages, normal text pages and pages coming from scanned documents. Text content can be easily extracted usin either PDFBox or Tika. However these libraries can't do OCR and I need something like <a href="http://tess4j.sourceforge.net/" rel="nofollow noreferrer">Tess4J</a>. But how can I combine Tess4J and PDFBox (or Tika) together in order to extract all the content from both text and scanned pages?</p>
  <p>EDITED:------
  I found a solution as follows but it doesn't work well</p>
  <pre><code>for(PDPage page:pages){
          
          PDPropertyList pageFonts = page.getResources().getProperties();
          Map&lt;String,PDXObjectImage&gt; img = page.getResources().getImages();
          
          Set&lt;String&gt; keys = img.keySet();
          Iterator iter = keys.iterator();
          
          while(iter.hasNext()){
              
              String k = (String) iter.next();
              System.out.println(k);              
              PDXObjectImage ci = img.get(k);
              
              ci.write2file(k);
              
              File imageFile = new File(k+&quot;.jpg&quot;);
              Tesseract instance = Tesseract.getInstance();
              
              
              try{
                  String result = instance.doOCR(imageFile);          
                  System.out.println(result);
              }
              catch(TesseractException e){
                  System.err.println(e.getMessage());
              }               
          }
      }
  </code></pre>
  <p>The problem is although image fies which are saved have good quality, Tess4J does not operate well on them and mostly extract nonsense. However Tess4J is able to OCR them very good if the pdf is simply passed to it in the first place.</p>
  <p>In summary, extracting the images by PdfBox affect the quality of OCR process, Does anyone know why?</p>