I am converting pptx slides to png by using POI library but it does not render correctly the slide’s colors, for example, instead of rendering turquoise it will render gray. I tried to convert pptx slides to svg but got the same problem, so I was wondering if it has something to do with XMLSlideShow class or XSLFSlide class. Any help will be greatly appreciated!
Code:
String file = "C:\\Users\\ABC\\demo1.pptx";
XMLSlideShow ppt = null;
ppt = new XMLSlideShow(OPCPackage.open(new File(file)));
Dimension pgsize = ppt.getPageSize();
float scale = 1;
int width = (int) (pgsize.width * scale);
int height = (int) (pgsize.height * scale);
XSLFSlide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
String title = slide[i].getTitle();
BufferedImage img = new BufferedImage(width, 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_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setColor(Color.white);
graphics.clearRect(0, 0, width, height);
graphics.scale(scale, scale);
slide[i].draw(graphics);
int sep = file.lastIndexOf(".");
String fname = file.substring(0, sep == -1 ? file.length() : sep) + "-" + (i + 1) +".png";
FileOutputStream out = new FileOutputStream(fname);
ImageIO.write(img, "png", out);
out.close();
}
This is a problem with apache POI library. Please use a older version i.e. 3.7 that might solve your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With