I used a free third-party library named Free Spire.Presentation for Java to convert PowerPoint documents to other formats, such as PDF, XPS, Image, and I wrote a relevant tutorial accordingly. About converting PPT to Image in that article, I used a method called ISlide.SaveAsImage() to convert a specific presentation slide to Image. Recently, I find that the library also supports converting each element in slides to image, such as such as chart, table, shape, textbox.
In
this article, I’ll take the following PowerPoint document including some
elements as an example, and then use Free Spire.Presentation for Java to convert them to several images.
We
still need to add Spire.Presentation.jar in the library to IDEA. You can get a
package from the link, unzip it and find the Jar file in the “lib” folder. Or
directly refer to it by using the following Maven configurations.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Using the code
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class ShapeAsImage {
public static void main(String[] args) throws Exception {
//Create a Presentation instance and load a PowerPoint document
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");
//Traverse every slides and every shapes on the slides
int k = 0;
for (int i = 0; i < presentation.getSlides().getCount(); i++) {
ISlide slide = presentation.getSlides().get(i);
for (int j = 0; j < slide.getShapes().getCount(); j++) {
String outputFile = "output/";
String fileName = outputFile + String.format("shapeToImage-%1$s.png", k);
//Save every single shape as image
BufferedImage image = slide.getShapes().saveAsImage(j);
ImageIO.write(image, "PNG", new File(fileName));
k++;
}
}
}
}
Output
No comments:
Post a Comment