代码存在一定错误。正确代码如下:
public static void main(String[] args) throws FileNotFoundException,
IOException {
final String PPT_TEMPLATE = "data/test.pptx";
float scale = 1;
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(PPT_TEMPLATE));
Dimension pgsize = ppt.getPageSize();
int width = (int) (pgsize.width * scale);
int height = (int) (pgsize.height * scale);
XSLFSlide slide = ppt.getSlides()[5];
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = img.createGraphics();
// 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.setColor(Color.white);
graphics.clearRect(0, 0, width, height);
graphics.scale(scale, scale);
// draw stuff
slide.draw(graphics);
// save the result
FileOutputStream out = new FileOutputStream(new File("D:/test.png"));
try {
ImageIO.write(img, "png", out);
} finally {
out.close();
}
System.out.println("Job Done");
}