title: PDFBox: extracting images from pdf to inputstream
body: <p>I am using PDFBox to extract the images from my pdf (which contains only jpg's).</p>
  
  <p>Since I will save those images inside my database, I would like to directly convert each image to an inputstream object first without placing the file temporary on my file sysem. I am facing difficulties with this however. I think it has to do because of the use of <code>image.getPDFStream().createInputStream()</code> as I did in the following example:</p>
  
  <pre><code>while (imageIter.hasNext()) {
      String key = (String) imageIter.next();
      PDXObjectImage image = (PDXObjectImage) images.get(key);
  
      FileOutputStream output = new FileOutputStream(new File(
              "C:\\Users\\Anton\\Documents\\lol\\test.jpg"));
      InputStream is = image.getPDStream().createInputStream(); //this gives me a corrupt file
      byte[] buffer = new byte[1024];
      while (is.read(buffer) &gt; 0) {
          output.write(buffer);
      }
  }
  </code></pre>
  
  <p>However this works:</p>
  
  <pre><code>while (iter.hasNext()) {
      PDPage page = (PDPage) iter.next();
      PDResources resources = page.getResources();
      Map&lt;String, PDXObject&gt; images = resources.getXObjects();
          if (images != null) {
              Iterator&lt;?&gt; imageIter = images.keySet().iterator();
              while (imageIter.hasNext()) {
              String key = (String) imageIter.next();
              PDXObjectImage image = (PDXObjectImage) images.get(key);
              image.write2file(new File("C:\\Users\\Anton\\Documents\\lol\\test.jpg")); //this works however
          }
      }
  }
  </code></pre>
  
  <p>Any idea how I can convert each PDXObjectImage (or any other object I can get) to an inputstream?</p>