Java文档开发组件推荐!Aspose助你快速完成PPTX到JPG转换

MS PowerPoint演示文稿不能直接从Web或桌面应用程序中显示。一种可能的解决方案是将PPTX演示文稿中的幻灯片转换为JPEG或PNG等图像。

>>你可以点击这里下载Aspose.Slides for Java v20.10测试体验。

PPT处理控件Aspose.Slides功能演示:使用Java将PPTX转换为JPG图像

  • 使用Java将PPTX转换为JPG图像
  • 在PPTX到JPG转换中自定义图像尺寸 

PPTX到JPG Java的转换

为了将PPTX幻灯片转换为JPG,可以简单地在演示文稿中循环浏览幻灯片并将它们另存为JPG文件。以下是执行此操作的步骤。

  • 使用Presentation类加载PowerPoint PPTX文件。
  • 使用Presentation.getSlides()方法检索演示文稿中的幻灯片。
  • 使用ISlide界面访问每张幻灯片。
  • 使用ISlide.getThumbnail(1f,1f)方法将幻灯片的缩略图获取到BufferedImage对象中。
  • 将BufferedImage另存为JPG文件。

下面的代码示例演示如何使用Java将PPTX幻灯片转换为JPG图像。

// Load the PowerPoint presentation
Presentation pres = new Presentation("presentation.pptx");

// Loop through each slide in the presentation
for (ISlide sld : pres.getSlides()) {
	
	// Create a full scale image
	BufferedImage bi = sld.getThumbnail(1f, 1f);

	// Create a new file
	File outputfile = new File(sld.getSlideNumber() + "_Slide.jpg");
	
	// Save the image to disk in JPEG format
	ImageIO.write(bi, "jpg", outputfile);
}

PowerPoint PPTX

PPT处理控件Aspose.Slides功能演示:使用Java将PPTX转换为JPG图像

转换后的JPG图像

PPT处理控件Aspose.Slides功能演示:使用Java将PPTX转换为JPG图像

PPTX到JPG –自定义图像尺寸

还可以根据需要自定义尺寸以缩放转换后的JPG图像。以下是在PPTX到JPG转换中指定Scale X和Scale Y值的步骤。

  • 使用Presentation类加载PowerPoint PPTX文件。
  • 分别使用幻灯片的宽度和高度指定x轴和y轴的缩放比例值。
  • 使用Presentation.getSlides()方法检索演示文稿中的幻灯片。
  • 使用ISlide界面访问每张幻灯片。
  • 使用ISlide.getThumbnail(ScaleX,ScaleY)方法将幻灯片的缩略图获取到BufferedImage对象中。
  • 将BufferedImage另存为JPG文件。

下面的代码示例演示如何在PPTX中将自定义缩放应用于JPG转换。

// Load the PowerPoint presentation
Presentation pres = new Presentation("presentation.pptx");

// Define dimensions
int desiredX = 1200;
int desiredY = 800;

// Get scaled values of X and Y
float ScaleX = (float)(1.0 / pres.getSlideSize().getSize().getWidth()) * desiredX;
float ScaleY = (float)(1.0 / pres.getSlideSize().getSize().getHeight()) * desiredY;

// Loop through each slide in the presentation
for (ISlide sld : pres.getSlides()) {
	
	// Create a full scale image
	BufferedImage bi = sld.getThumbnail(ScaleX, ScaleY);

	// Create a new file
	File outputfile = new File(sld.getSlideNumber() + "_Slide.jpg");
	
	// Save the image to disk in JPEG format
	ImageIO.write(bi, "jpg", outputfile);
}

如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询。