问题
When I convert ppt to png using follow code:
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");
}
And I can't get the correct PNG.
first picture is a slide from ppt and second picture is the result after converting.
How can I get the correct result?
[

And I have confirmed that conversion chart can't display.
回答1:
I use POI 3.10 for the same kind of purpose, and I've found out that all things embedded in the PPT file cannot be converted (including Cliparts, SmartArt, WordArt, Tables, and external Files).
Only images pasted in the file will be included in the converted file.
回答2:
TutorialPoint has a very straight forward but with little tweaking tutorial to do that HERE. I modified to display a full pptx document on JFrame found the code below:
try {
outFile = new File("pages/" + fileName); //fileName here is the name of the folder to save all the slides
if (!outFile.exists()) {
outFile.mkdir();
//creating an empty presentation
XMLSlideShow ppt = new XMLSlideShow(is);
//getting the dimensions and size of the slide
Dimension pgsize = ppt.getPageSize();
XSLFSlide[] slide = ppt.getSlides();
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < slide.length; i++) {
Graphics2D graphics = img.createGraphics();
//clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
//render
slide[i].draw(graphics);
//creating an image file as output
System.out.println(outFile.getName() + "/" + i + ".png");
OutputStream out = new FileOutputStream("pages/" + fileName + "/" + i + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
}
}
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
if (outFile.exists() && outFile.isDirectory()) { //Always a good practice to check to avoid errors
final FilenameFilter IMAGE_FILTER = new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
if (name.endsWith(".png")) //make sure only png are valid
return (true);
else
return (false);
}
};
for (final File f : outFile.listFiles(IMAGE_FILTER)) {//fetch all png files within the folder
panel.add(new JLabel(new ImageIcon(f.getAbsolutePath())));
}
}
JScrollPane labelScrollPane = new JScrollPane(panel); //Wrapped in scrollpane just in case the slides are much
add(labelScrollPane); //Add to frame or panel
来源:https://stackoverflow.com/questions/33161979/convert-ppt-to-png-with-apache-poi