Java 使用apache poi将ppt转换为png
当我使用以下代码将ppt转换为png时:Java 使用apache poi将ppt转换为png,java,apache-poi,powerpoint,Java,Apache Poi,Powerpoint,当我使用以下代码将ppt转换为png时: 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_TEM
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");
}
我不能得到正确的PNG
第一张图片是ppt中的幻灯片,第二张图片是转换后的结果
我怎样才能得到正确的结果
[
我已经确认转换图表无法显示。我使用POI 3.10也是出于同样的目的,我发现PPT文件中嵌入的所有内容都无法转换,包括剪贴画、SmartArt、WordArt、表格和外部文件
只有粘贴到文件中的图像才会包含在转换后的文件中。TutorialPoint有一个非常简单的教程,但没有做什么调整。我修改了它,在JFrame上显示完整的pptx文档,找到以下代码:
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
图表是图片还是嵌入式Excel文件?您使用的是最新版本的Apache POI吗?如果不是,升级时会发生什么?它是嵌入式文件,我使用POI-3.9.jar