尽管您可以参考 POI 项目本身的一些示例代码,但您应该寻找以下内容;希望这可以帮助;
private ImageIcon generateFromPPTX(int index) {
ImageIcon icon = null;
XMLSlideShow slideShowPPTX = null;
FileInputStream pptInputStream = null;
XSLFSlide [] allSlides = null;
XSLFSlide singleSlide = null;
BufferedImage memoryImage = null;
Graphics2D graphics = null;
try{
pptInputStream = new FileInputStream("somepptx-file.pptx");
slideShowPPTX = new XMLSlideShow(pptInputStream);
allSlides = slideShowPPTX.getSlides();
if(allSlides == null || allSlides.length == 0) {
System.out.println("Empty presentation!");
return null;
}
singleSlide = allSlides [index];
memoryImage = new BufferedImage(slideShowPPTX.getPageSize().width, slideShowPPTX.getPageSize().height, BufferedImage.TYPE_INT_ARGB);
graphics = memoryImage.createGraphics();
// Only few rendering hints set but you can set as many as you need depending on your need
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
singleSlide.draw(graphics);
icon = new ImageIcon(memoryImage);
}
catch(IOException exception){
System.err.println("Input/output Exception:");
exception.printStackTrace();
}
finally{
slideShowPPTX = null;
allSlides = null;
singleSlide = null;
memoryImage = null;
graphics = null;
if(pptInputStream != null){
try {
pptInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
pptInputStream = null;
}
}
return icon;
}