Bug 63460 - Exception thrown when converting powerpoint files to images
Summary: Exception thrown when converting powerpoint files to images
Status: NEEDINFO
Alias: None
Product: POI
Classification: Unclassified
Component: SL Common (show other bugs)
Version: unspecified
Hardware: PC Mac OS X 10.1
: P2 critical (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-05-23 11:08 UTC by Alloy
Modified: 2019-06-20 08:27 UTC (History)
0 users



Attachments
Converted gradient slide [greyed] (430.39 KB, image/png)
2019-05-23 11:08 UTC, Alloy
Details
pptx file that i have used (611.45 KB, application/vnd.openxmlformats-officedocument.presentationml.presentation)
2019-05-24 05:40 UTC, Alloy
Details

Note You need to log in before you can comment on or make changes to this bug.
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".