Bug 63460

Summary: Exception thrown when converting powerpoint files to images
Product: POI Reporter: Alloy <aliy.invoker>
Component: SL CommonAssignee: POI Developers List <dev>
Status: RESOLVED WORKSFORME    
Severity: critical    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X 10.1   
Attachments: Converted gradient slide [greyed]
pptx file that i have used

Description Alloy 2019-05-23 11:08:52 UTC
Created attachment 36596 [details]
Converted gradient slide [greyed]

//    Exception in thread "main" java.lang.IllegalArgumentException: Color 
//    parameter outside of expected range: Red

//    Main converter from both pptx and ppt

import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class Application {


    public static void main(String[] args) throws Exception {

        System.out.println("working");

        String directory = System.getProperty("user.home") + "/Desktop/";

        //creating an empty presentation
        File file=new File(directory + "test6.pptx");
        String fileName = file.getName();
        // Identify the file whether it is ppt or pptx
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1);

        FileInputStream inputStream = new FileInputStream(directory +  fileName);

        if (extension.equals("pptx")) {


            XMLSlideShow pptx = new XMLSlideShow(inputStream);

            //getting the dimensions and size of the slide
            Dimension pgsize = pptx.getPageSize();
            java.util.List<XSLFSlide> pptxSlide = Arrays.asList(pptx.getSlides());

            for (int i = 0; i < pptxSlide.size(); i++) {
                BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,BufferedImage.TYPE_INT_RGB);

                Graphics2D graphics = createImage(img, pgsize);
                //render
                pptxSlide.get(i).draw(graphics);
                graphics.setComposite(AlphaComposite.DstOver);
                graphics.setPaint(Color.WHITE);

                //creating an image file as output
                imageFile(i, img);


                graphics.dispose();

            }


        }else if(extension.equals("ppt")){


            SlideShow ppt = new SlideShow(inputStream);

            //getting the dimensions and size of the slide
            Dimension pgsize = ppt.getPageSize();
            List<Slide> slide = Arrays.asList(ppt.getSlides());

            for (int i = 0; i < slide.size(); i++) {
                BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,BufferedImage.TYPE_INT_RGB);

                //render
                slide.get(i).draw(createImage(img, pgsize));


                //creating an image file as output
                imageFile(i, img);
            }
        }else {
            throw new Exception("Not compatible type ");
        }
    }

    private static void imageFile(int i, BufferedImage img) throws IOException {
        FileOutputStream out = new FileOutputStream("ppt_image_" + i + ".png");
        javax.imageio.ImageIO.write(img, "png", out);
        System.out.println("Image successfully created");
        out.close();
    }


    private static Graphics2D createImage(BufferedImage img, Dimension pgsize){
        Graphics2D graphics = img.createGraphics();

        //clear the drawing area
        graphics.setPaint(Color.white);
        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
        // default rendering options
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        graphics.setComposite(AlphaComposite.DstOver);


        return graphics;
    }
}
Comment 1 Andreas Beeker 2019-05-23 11:15:06 UTC
For testing I'll use the PPTX2PNG class [1], which is basically the same as your code. As we haven't got that error on our huge test corpus (which is only used before we do a release), I guess the color reference to "Red" was generated by a custom tool, right? (i.e. do you know the origin of the .pptx file?)

[1]
https://svn.apache.org/repos/asf/poi/trunk/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java
Comment 2 Andreas Beeker 2019-05-23 12:03:05 UTC
I've just realized, that you've uploaded the .png file ... but I've asked for the .pptx on stackoverflow [2]. I can generate the .png myself - please upload the .pptx.


[2] https://stackoverflow.com/questions/56221004/gradient-color-issue-when-converting-from-pptx-to-images-apache-poi
Comment 3 Alloy 2019-05-24 05:40:48 UTC
Created attachment 36598 [details]
pptx file that i have used
Comment 4 Andreas Beeker 2019-05-26 17:55:36 UTC
Your provided file is not throwing the error. I'm using the trunk version of POI, which should be similar to the latest released stable version. I've tested with the PPTX2PNG class and also with the code you've provided ... in your case, I would also use PPTX2PNG, because your code produces white images.
I guess the failing file is "test6.pptx" instead of "test7.pptx".
Comment 5 Dominik Stadler 2021-02-01 09:27:48 UTC
We could not reproduce this, so I am closing this for now, please reopen with some more information if this still happens with the latest version Apache POI 5.0.0.