900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 将Java中的PPT(X)转换为PDF和图像

将Java中的PPT(X)转换为PDF和图像

时间:2020-08-22 22:59:55

相关推荐

将Java中的PPT(X)转换为PDF和图像

与PowerPoint相比,PDF(或图像)文件格式的优点是:

PDF /图像文件固定在页面布局中并且难以修改,因此更适合归档。由于PDF /图像文件与大多数设备兼容,因此交付更加方便。

For above reasons, you would probably convert your PowerPoint document into a PDF file or multiple image files. This article will show you how to accomplish this task by using Spire.Presentation for Java.

下面是输入PowerPoint文档的屏幕截图。

PPT(X) to PDF

使用Spire.Presentation从PowerPoint到PDF的转换非常容易。 只需创建Presentation类的对象来保存要转换的PowerPoint文件,然后调用同一对象的saveToFile()方法即可将文档另存为PDF文件。

import com.spire.presentation.Presentation;import com.spire.presentation.FileFormat;public class PPTXtoPDF {public static void main(String[] args) throws Exception {//create a Presentataion instancePresentation presentation = new Presentation();//load the sample PowerPoint filepresentation.loadFromFile("C:/Users/Administrator/Desktop/template.pptx");//save to PDF filepresentation.saveToFile("ToPDF.pdf", FileFormat.PDF);presentation.dispose();}}

PPT(X) to PNG

要将PowerPoint幻灯片导出为图像,首先需要使用saveAsImage()方法将每张幻灯片保存为BufferdImage,然后将图像数据写入.png文件格式的图像文件中。

import com.spire.presentation.Presentation;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;public class ConvertPPTXtoPNG {public static void main(String[] args) throws Exception {//create a Presentation object Presentation presentation = new Presentation();//load an example PPTX file presentation.loadFromFile("C:/Users/Administrator/Desktop/template.pptx");//loop through the slides for (int i = 0; i < presentation.getSlides().getCount(); i++) {//save each slide as a BufferedImage BufferedImage image = presentation.getSlides().get(i).saveAsImage();//save BufferedImage as PNG file format String fileName = String.format("ToImage-%1$s.png", i);ImageIO.write(image, "PNG",new File(fileName));}presentation.dispose();}}

from: https://dev.to//eiceblue/convert-pptx-to-pdf-and-images-in-java-49cc

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。