title: How to use Tesseract
body: <p>I'm trying to convert the images in a PDF file <em>(every page is a scanned image)</em> to text, using Tesseract (Tess4J) OCR, but it's not working: <em>(see comments in code)</em></p>
  
  <pre><code>public static void main(String[] args) throws IOException
  {
      Tesseract instance = Tesseract.getInstance();  // JNA Interface Mapping
  
      PdfReader reader = new PdfReader(PREFACE); // a PDF File D:\pdf\test.pdf
      int n = reader.getXrefSize();
      PdfObject object;
      PRStream stream;
  
      // Looking for image and manipulating image stream
      for (int i = 0; i &lt; n; i++)
      {
          object = reader.getPdfObject(i); // every time object null ?why
          if (object == null || !object.isStream())
              continue;
  
          stream = (PRStream)object;
  
          PdfImageObject image = new PdfImageObject(stream);
          BufferedImage bi = image.getBufferedImage();
  
          try {
              String result = instance.doOCR(bi); //take Buffered iamge to transfor ti 
              System.out.println(result);  
          } catch (TesseractException e) {
              System.err.println(e.getMessage());
              stream.clear();
          }
      }
  }
  </code></pre>