我已经使用apache poi为图像创建了pptx和ppt转换器,但是在转换某些文件时,经过两次或三次转换后出现了异常。
线程“ main”中的异常java.lang.IllegalArgumentException:颜色参数超出预期范围:红色
pptx和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;
}
}
主要问题是我想将任何具有渐变色或简单颜色的pptx文件转换为图像。
已转换的渐变幻灯片[灰色]