title: Convert pptx to images using java
body: <p>i try to write a java program, which converts the pptx to jpeg images. There are many options (e.g., aspose, groupdocs, cloudmersive). But they are limited (either generating images in the evaluation mode or limited by the number of API calls); i prefer an open source library and up till now, i have known 2 libreries which are Apache POI and Docx4j. By using the Apache POI, i was able to implement the conversion PPTX-JPEG; however, the generated images' quality is low even the rendering parameters were set.</p>
  <pre><code>// create an empty presentation
          public static void Pptx_Img(String slidePath, String outputDir, String fileName) throws Exception {
          // create an empty presentation
          File file = new File(slidePath);
          FileInputStream in = new FileInputStream(file);
          XMLSlideShow ppt = new XMLSlideShow(in);
   
          // get the dimension and size of the slide
          Dimension pgsize = ppt.getPageSize();
          List&lt;XSLFSlide&gt; slides = ppt.getSlides();
          
          JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
          jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
          jpegParams.setCompressionQuality(1f);
          
          BufferedImage img = null;
          
          outputDir = outputDir + &quot;\\apache\\pptx\\&quot; + fileName;
          Apache.createDirIfNotExists(outputDir);
   
          for (int i = 0; i &lt; 10; i++) {
              img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
              Graphics2D graphics = img.createGraphics();
              graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
              graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
              graphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
              graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
              graphics.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
              graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
              graphics.setRenderingHint(Drawable.BUFFERED_IMAGE, new WeakReference&lt;&gt;(img));
              
              // clear area
              graphics.setPaint(Color.white);
              graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
               
              // draw the images
              slides.get(i).draw(graphics);
              FileOutputStream out = new FileOutputStream(outputDir + &quot;\\slide_&quot; + i + &quot;.jpg&quot;);
              
              /*
              // specifies where the jpg image has to be written
              final ImageWriter writer = ImageIO.getImageWritersByFormatName(&quot;jpg&quot;).next();
              writer.setOutput(ImageIO.createImageOutputStream(out));
      
              // writes the file with given compression level 
              writer.write(null, new IIOImage(img, null, null), jpegParams);
              */
  
              ImageIO.write(img, &quot;jpeg&quot;, out);
              
              ppt.write(out);
              graphics.dispose();
              out.close();
              img.flush();
          }
          System.out.println(&quot;Images successfully created&quot;);
          in.close();
          ppt.close();
      }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/ib87H.png" rel="nofollow noreferrer">Comparison between the original slide and the generated images</a></p>
  <p>I also came up with another indirect way, which converts pptx-pdf-jpeg (because i have the implementation for conversion pdf-jpeg and the conversion can actually maintain the image quality). But i couldnt find any open source libs which can do the conversion pptx-jpeg. For Apache POI, it can convert pptx-pdf but the mechanism is just similar to pptx-jpeg: writes the Graphic2D-instance to pdf instead of jpeg, which basically does not improve the rendering quality.</p>
  <hr />
  <p>Added: Regarding to the comparison, the wrong rendering is also another issue !</p>