有没有一种使用Java编程将幻灯片拆分为.png文件的方法?我搜索过,大多数答案都是用C#编写的,或者提到的程序不是开源的。
有没有一种使用Java编程将幻灯片拆分为.png文件的方法?我搜索过,大多数答案都是用C#编写的,或者提到的程序不是开源的。
要获得良好的质量,请使用Apache POI HSLF库中的以下代码(http://poi.apache.org/slideshow/how-to-shapes.html):
FileInputStream is = new FileInputStream("path_to_your.ppt");
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, 1);
Graphics2D graphics = img.createGraphics();
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, pgsize.width, pgsize.height);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// render
slide[i].draw(graphics);
// save the output
FileOutputStream out = new FileOutputStream("slide-" + (i + 1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
}
您需要使用Java/COM桥接器,例如j-interop (http://www.j-interop.org/) 来编程控制PowerPoint进程,然后可能将单个页面打印到文件中。您最好只是编写一个VBA脚本。
使用Apache POI库,可使用以下代码:
FileInputStream is = new FileInputStream("D:\\PPT sample.ppt");
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
XSLFSlide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.SCALE_SMOOTH);
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);
//save the output
FileOutputStream out = new FileOutputStream("D:\\slide-" + (i+1) + ".JPG");
javax.imageio.ImageIO.write(img, "JPG", out);
out.close();