如何使用 Java 创建 PowerPoint 幻灯片图像

此操作方法主题是关于如何使用 Java** 创建 PowerPoint 幻灯片图像以及详细的逐步信息和工作示例代码。此应用程序可用于从 Java 中的 PowerPoint 幻灯片生成图像**,而不依赖于 MS PowerPoint 或任何其他第三方工具。此外,该应用程序可以在任何支持 Java 的环境中使用,包括 MS Windows、macOS 或 Linux。

使用 Java 创建 PowerPoint 幻灯片图像的步骤

  1. 通过使用 Maven 存储库安装 Aspose.Slides for Java 来配置应用程序
  2. 使用 Presentation 类对象打开示例演示文件以生成幻灯片图像
  3. 加载演示幻灯片集合中的第一个 slide
  4. 定义幻灯片缩略图的自定义尺寸
  5. 创建幻灯片缩略图并以 JPG 格式保存在磁盘上

为了开发基于 Java* 的 * 演示图像创建器,执行定义的步骤的代码已在上面演示。该过程通过从磁盘加载源演示文件开始,然后从幻灯片集合中访问所需的幻灯片。最后,通过使用 getThumbnail() 方法中的自定义尺寸,幻灯片在磁盘上呈现为 JPEG 图像。

在 Java 中从 PowerPoint 幻灯片生成图像的代码

import com.aspose.slides.ISlide;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class CreateSlideImage {
public static void main2(String[] args) throws Exception
{
// Set upt the a license for Aspsoe.Slides
License licForCSlides = new License();
licForCSlides.setLicense("Aspose.Total.lic");
// Create the Presentation class object to load the source presentation file
Presentation samplePresentation = new Presentation("NewPresentation.pptx");
// Accessing the first slide
ISlide srcSlide = samplePresentation.getSlides().get_Item(0);
// Create the user defined dimension
int desiredXdim = 1200;
int desiredYdim = 800;
// Getting scaling factor value of X and Y
float scaleX = (float)(1.0 / samplePresentation.getSlideSize().getSize().getWidth()) * desiredXdim;
float scaleY = (float)(1.0 / samplePresentation.getSlideSize().getSize().getHeight()) * desiredYdim;
// Generating the buffered image for slide
BufferedImage slideBmpImage = srcSlide.getThumbnail(scaleX, scaleY);
// Now create the slide JPEG image on the disk
ImageIO.write(slideBmpImage, "JPEG", new File("Slide_0.jpeg"));
}
}

上面的示例演示了在 JPG 中使用Java 代码在简单的 API 调用的帮助下将 PowerPoint 幻灯片转换为图像。您可以在磁盘上将生成的缩略图呈现为 PNG、BMP 或其他图像格式。您还可以通过使用 getThumbnail() 方法的不同重载设置不同的呈现选项(如 DefaultRegularFont、TiffOptions、NotesCommentsLayouting、大小和图像缩放)来自定义幻灯片图像。

在本主题中,我们重点介绍了如何使用简单易懂的代码将 PowerPoint 转换为 Java 中的 JPG。如果您想了解如何将演示文稿转换为 HTML,请参阅 如何使用 Java 在 HTML 中创建 PowerPoint 幻灯片 上的文章。

 简体中文