与 PowerPoint 文档相比,图像文件更易于查看,因为它们几乎可以在任何设备上打开,而无需特定的软件。如果您想在各种设备上访问PowerPoint文档,可以将其转换为图像。在本文中,本文将介绍如何使用 Spire.Presentation for Java 将 PowerPoint 文档转换为各种图像格式。
安装 Spire.Presentation for Java
4首先,您需要在 Java 程序中添加 Spire.Presentation.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而轻松地在应用程序中导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>10.1.3</version>
</dependency>
</dependencies>
将 PowerPoint 文档转换为 JPG 或 PNG 图像
Spire.Presentation for Java 拥有强大的文档转换功能,可以使您快速地将 PowerPoint 文档转换图像。以下是将 PowerPoint 文档转换为 JPG 或 PNG 图像的具体步骤。
- 初始化 Presentation 类的实例。
- 使用 Presentation.loadFromFile() 方法加载 PowerPoint 文档。
- 遍历 PowerPoint 文档中的所有幻灯片。
- 使用 ISlide.saveAsImage() 方法将每张幻灯片另存为 BufferedImage 对象。
- 使用 ImageIO.write() 方法将 BufferedImage 对象保存为 PNG 或 JPG 文件。
- Java
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 ConvertPowerPointToPngOrJpg {
public static void main(String []args) throws Exception {
//初始化Presentation类的实例
Presentation presentation = new Presentation();
//加载PowerPoint文档
presentation.loadFromFile("示例文档.pptx");
//遍历PowerPoint文档中的所有幻灯片
for(int i = 0; i < presentation.getSlides().getCount(); i++)
{
ISlide slide = presentation.getSlides().get(i);
//将每张幻灯片另存为PNG图像
BufferedImage image = slide.saveAsImage();
String fileName = String.format("ToImage-%1$s.png", i);
ImageIO.write(image, "PNG",new File(fileName));
}
}
}
将 PowerPoint 文档转换为 TIFF 图像
Spire.Presentation for Java 提供了直接的方法用于加载文档以及保存为目标文档格式,仅需3步即可轻松地将 PowerPoint 文档转换为 TIFF 图像。您可以按照下面列出的步骤,实现此功能。
- 初始化 Presentation 类的实例。
- 使用 Presentation.loadFromFile() 方法加载 PowerPoint 文档。
- 使用 Presentation.saveToFile(String, FileFormat) 方法将 PowerPoint 文档转换为 TIFF 图像。
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class ConvertPowerPointToTiff {
public static void main(String []args) throws Exception {
//初始化Presentation类的实例
Presentation presentation = new Presentation();
//加载PowerPoint文档
presentation.loadFromFile("示例文档.pptx");
//将PowerPoint文档转换为TIFF图像
presentation.saveToFile("toTIFF.tiff", FileFormat.TIFF);
}
}
将 PowerPoint 文档转换为 SVG 图像
使用 Spire. Presentation for Java,您可以将 Presentation 文档保存为字节数组列表。然后,每个字节数组都可以写成一个 SVG 文件。将 Presentation 转换为 SVG 的详细步骤如下。
- 初始化 Presentation 类的实例。
- 使用 Presentation.loadFromFile() 方法加载 PowerPoint 文档。
- 将 PowerPoint 文档转换为 SVG,并使用 Presentation.saveToSVG() 方法将结果保存到字节数组的 ArrayList 中。
- 遍历 ArrayList 中的字节数组。
- 使用 ArrayList.get(int) 方法获取当前字节数组。
- 初始化 FileOutputStream 类的实例,并使用 FileOutputStream.write() 方法将字节数组保存到 SVG 文件中。
- Java
import com.spire.presentation.Presentation;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class ConvertPowerPointToSVG {
public static void main(String []args) throws Exception {
//创建Presentation对象
Presentation presentation = new Presentation();
//加载示例PowerPoint文档
presentation.loadFromFile("示例文档.pptx");
//将PowerPoint文档转换为SVG格式,并以byte数组的形式保存于ArrayList
ArrayList svgBytes =(ArrayList) presentation.saveToSVG();
int len = svgBytes.size();
//遍历ArrayList中的字节数组
for (int i = 0; i < len; i++)
{
//获取当前字节数组
byte[] bytes = (byte[]) svgBytes.get(i);
//指定输出文件名
String fileName= String.format("ToSVG-%d.svg", i);
//创建FileOutputStream实例
FileOutputStream stream = new FileOutputStream(fileName);
//将字节数组保存到SVG文件
stream.write(bytes);
}
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。