900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Java POI实现pptpptx转换为pdf文件

Java POI实现pptpptx转换为pdf文件

时间:2023-06-04 10:30:25

相关推荐

Java POI实现pptpptx转换为pdf文件

Java POI实现ppt&pptx转换为pdf文件

背景区别参考博客代码依赖工具类

背景

需求使用ppt&pptx 上传文件转换为PDF文件,方便前端展示

区别

参考博客

java 实现 ppt或pptx文件转换PDF文件 – poi

代码

依赖

<!-- poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.15</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.15</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.15</version></dependency><!-- itextpdf --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.4.3</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

工具类

与参考博客差不多。做了一些细小处理

package com.fang.mon.utils;import java.awt.*;import java.awt.image.BufferedImage;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.List;import mons.lang3.StringUtils;import org.apache.poi.xslf.usermodel.*;import org.apache.poi.xssf.usermodel.*;import org.apache.poi.hslf.usermodel.*;import com.itextpdf.text.Document;import com.itextpdf.text.pdf.*;import com.itextpdf.text.Image;/*** @author: guochao.bj@* @createDate: /11/30 13:52*/public class PdfConverUtil {public static boolean pptxToPdf(String pptPath, String pdfDir) {if (StringUtils.isEmpty(pptPath)) {throw new RuntimeException("word文档路径不能为空");}if (StringUtils.isEmpty(pdfDir)) {throw new RuntimeException("pdf目录不能为空");}String pdfPath = pdfDir + "te." + "pdf";Document document = null;XMLSlideShow slideShow = null;FileOutputStream fileOutputStream = null;PdfWriter pdfWriter = null;try {//使用输入流pptx文件slideShow = new XMLSlideShow(new FileInputStream(pptPath));//获取幻灯片的尺寸Dimension dimension = slideShow.getPageSize();//新增pdf输出流,准备讲ppt写出fileOutputStream = new FileOutputStream(pdfPath);//创建一个写内容的容器document = new Document();//使用输出流写入pdfWriter = PdfWriter.getInstance(document, fileOutputStream);//使用之前必须打开<You have to open the document before you can write content.>document.open();PdfPTable pdfPTable = new PdfPTable(1);//获取幻灯片List<XSLFSlide> slideList = slideShow.getSlides();for (int i = 0, row = slideList.size(); i < row; i++) {//获取每一页幻灯片XSLFSlide slide = slideList.get(i);for (XSLFShape shape : slide.getShapes()) {//判断是否是文本if(shape instanceof XSLFTextShape){// 设置字体, 解决中文乱码XSLFTextShape textShape = (XSLFTextShape) shape;for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {for (XSLFTextRun textRun : textParagraph.getTextRuns()) {textRun.setFontFamily("宋体");}}}}//根据幻灯片尺寸创建图形对象BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);Graphics2D graphics2d = bufferedImage.createGraphics();graphics2d.setPaint(Color.white);graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));//把内容写入图形对象slide.draw(graphics2d);graphics2d.dispose();//封装到Image对象中Image image = Image.getInstance(bufferedImage, null);image.scalePercent(50f);// 写入单元格pdfPTable.addCell(new PdfPCell(image, true));document.add(image);}} catch (Exception e) {e.printStackTrace();return false;} finally {try {if (document != null) {document.close();}if (fileOutputStream != null) {fileOutputStream.close();}if (pdfWriter != null) {pdfWriter.close();}} catch (IOException e) {e.printStackTrace();}}return true;}public static boolean pptToPdf(String pptPath, String pdfDir) {if (StringUtils.isEmpty(pptPath)) {throw new RuntimeException("word文档路径不能为空");}if (StringUtils.isEmpty(pdfDir)) {throw new RuntimeException("pdf目录不能为空");}String pdfPath = pdfDir + "te." + "pdf";Document document = null;HSLFSlideShow hslfSlideShow = null;FileOutputStream fileOutputStream = null;PdfWriter pdfWriter = null;try {//使用输入流ppt文件hslfSlideShow = new HSLFSlideShow(new FileInputStream(pptPath));// 获取ppt文件页面Dimension dimension = hslfSlideShow.getPageSize();fileOutputStream = new FileOutputStream(pdfPath);document = new Document();// pdfWriter实例pdfWriter = PdfWriter.getInstance(document, fileOutputStream);document.open();PdfPTable pdfPTable = new PdfPTable(1);List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides();for (int i=0; i < hslfSlideList.size(); i++) {HSLFSlide hslfSlide = hslfSlideList.get(i);for (HSLFShape shape : hslfSlide.getShapes()) {if (shape instanceof HSLFTextShape){// 设置字体, 解决中文乱码HSLFTextShape textShape = (HSLFTextShape) shape;for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {for (HSLFTextRun textRun : textParagraph.getTextRuns()) {textRun.setFontFamily("宋体");}}}}BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);Graphics2D graphics2d = bufferedImage.createGraphics();graphics2d.setPaint(Color.white);graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));hslfSlide.draw(graphics2d);graphics2d.dispose();Image image = Image.getInstance(bufferedImage, null);image.scalePercent(50f);// 写入单元格pdfPTable.addCell(new PdfPCell(image, true));document.add(image);}} catch (Exception e) {e.printStackTrace();return false;} finally {try {if (document != null) {document.close();}if (fileOutputStream != null) {fileOutputStream.close();}if (pdfWriter != null) {pdfWriter.close();}} catch (IOException e) {e.printStackTrace();}}return true;}public static void main(String[] args) {boolean successful = false;// ppt to pdfsuccessful = pptxToPdf("D:\\test1.pptx", "D:\\");// pptx to pdf// successful = PdfConvertUtil.pptxToPdf("D:\\360_js\\测321pt.pptx", "D:\\360_js");System.out.println("转换" + (successful ? "成功" : "失败"));}}

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