JAVA实现把PPT转PDF的方法
文章目录
- 前言
- 一、Apache poi是什么?
- 二、具体实现
- 1. 引入依赖
- 2. ppt/pptx转换pdf (返回InputStream)
- 3. ppt/pptx转换pdf (返回pdf文件)
前言
项目里前端想要上传的ppt转成pdf之后上传oss,好方便前端在页面中预览。找了好几个ppt转pdf的方案, 最终选择Apache poi
- 使用jacob可以将office文件转换成pdf,因为需要依赖Microsoft Office,适用于windows服务器部署的项目。(项目部署在linux服务器上, 所以排除了这个方案)
- 如果需要用Linux服务器,请考虑使用OpenOffice方案 (因为这个方案需要在服务器上安装OpenOffice, 所以也排除了)
- Apache poi 也就是本次的主角
一、Apache poi是什么?
- Apache POI 简介是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office(Excel、WORD、PowerPoint、Visio等)格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“可怜的模糊实现”。
二、具体实现
1. 引入依赖
<!-- poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.2</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>
2. ppt/pptx转换pdf (返回InputStream)
由于ppt和pptx文件格式不同,ppt是基于二进制的文件,而pptx是基于xml文件, 也是就pptx是2007年后出现的新的ppt版本,对这两种文件处理方式转换PDF其实都差不多,只是要注意接收文件ppt或pptx以及获取两种文件内容 需要的类处理,即使用POI 里面的XMLSlide 和 HSLFSlide 进行分别处理。
因为最后是要把文件上传到阿里云oss, 需要的是InputStream, 所以这里返回的是转化后pdf的InputStream
/*** @author ErrorRua* @date 2022/11/17* @description: 转换PPT为PDF工具类*/
public final class PdfConvertUtil {/*** @description: pptxToPdf* @param pptIs:* @return java.io.InputStream* @author ErrorRua* @date 2022/11/21*/public static InputStream pptToPdf(InputStream pptIs) {Document document = null;PdfWriter pdfWriter = null;try (HSLFSlideShow hslfSlideShow = new HSLFSlideShow(pptIs);ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {// 获取ppt文件页面Dimension dimension = hslfSlideShow.getPageSize();document = new Document();// pdfWriter实例pdfWriter = PdfWriter.getInstance(document, byteArrayOutputStream);document.open();PdfPTable pdfPTable = new PdfPTable(1);List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides();for (HSLFSlide hslfSlide : hslfSlideList) {// 设置字体, 解决中文乱码for (HSLFShape shape : hslfSlide.getShapes()) {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 Font("宋体", 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);}return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());} catch (Exception e) {e.printStackTrace();return null;} finally {if (document != null) {document.close();}if (pdfWriter != null) {pdfWriter.close();}}}/*** @description: pptxToPdf* @param pptIs:* @return java.io.InputStream* @author ErrorRua* @date 2022/11/21*/public static InputStream pptxToPdf(InputStream pptIs) {Document document = null;PdfWriter pdfWriter = null;try(ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();XMLSlideShow slideShow = new XMLSlideShow(pptIs);) {Dimension dimension = slideShow.getPageSize();document = new Document();pdfWriter = PdfWriter.getInstance(document, byteArrayOutputStream);document.open();PdfPTable pdfPTable = new PdfPTable(1);List<XSLFSlide> slideList = slideShow.getSlides();for (XSLFSlide slide : slideList) {// 设置字体, 解决中文乱码for (XSLFShape shape : slide.getShapes()) {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 Font("宋体", Font.PLAIN, 12));slide.draw(graphics2d);graphics2d.dispose();Image image = Image.getInstance(bufferedImage, null);image.scalePercent(50f);// 写入单元格pdfPTable.addCell(new PdfPCell(image, true));document.add(image);}return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());} catch (Exception e) {e.printStackTrace();return null;} finally {if (document != null) {document.close();}if (pdfWriter != null) {pdfWriter.close();}}}}
3. ppt/pptx转换pdf (返回pdf文件)
import cn.hutool.core.util.StrUtil;import java.awt.*;
import java.awt.image.BufferedImage;import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.hslf.usermodel.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.Image;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;/*** 类名称:PdfConvertUtil* 类描述:转为为PDF工具类
*/
public final class PdfConverUtil {/*** pptToPdf* @param pptPath PPT文件路径* @param pdfDir 生成的PDF文件路径* @return*/public static boolean pptToPdf(String pptPath, String pdfDir) {if (StrUtil.isEmpty(pptPath)) {throw new RuntimeException("word文档路径不能为空");}if (StrUtil.isEmpty(pdfDir)) {throw new RuntimeException("pdf目录不能为空");}String pdfPath = pdfDir + StrUtil.sub(pptPath, pptPath.lastIndexOf(StrUtil.BACKSLASH), pptPath.lastIndexOf(StrUtil.DOT)) + StrUtil.DOT + "pdf";Document document = null;HSLFSlideShow hslfSlideShow = null;FileOutputStream fileOutputStream = null;PdfWriter pdfWriter = null;try {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()) {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;}/**** @Title: pptxToPdf* @param pptPath PPT文件路径* @param pdfDir 生成的PDF文件路径*/public static boolean pptxToPdf(String pptPath, String pdfDir) {if (StrUtil.isEmpty(pptPath)) {throw new RuntimeException("word文档路径不能为空");}if (StrUtil.isEmpty(pdfDir)) {throw new RuntimeException("pdf目录不能为空");}String pdfPath = pdfDir + StrUtil.sub(pptPath, pptPath.lastIndexOf(StrUtil.BACKSLASH), pptPath.lastIndexOf(StrUtil.DOT)) + StrUtil.DOT + "pdf";Document document = null;XMLSlideShow slideShow = null;FileOutputStream fileOutputStream = null;PdfWriter pdfWriter = null;try {slideShow = new XMLSlideShow(new FileInputStream(pptPath));Dimension dimension = slideShow.getPageSize();fileOutputStream = new FileOutputStream(pdfPath);document = new Document();pdfWriter = PdfWriter.getInstance(document, fileOutputStream);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()) {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.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;}}
相关文章:
- java 实现 ppt或pptx文件转换PDF文件 – poi
- jacob 实现 ppt或pptx文件转换PDF文件
- aspose.slides实现 ppt或pptx文件转换PDF文件
JAVA实现把PPT转PDF的方法相关推荐
- 怎么把ppt转成pdf?PPT转PDF简单方法分享
我们平时在工作办公的时候,Word,Excel,PPT,PDF这些文件格式都会经常用到,文件格式之间的转换也会经常碰到.PPT是我们在演讲或开会时会经常用到的,但是有时候我们在其他设备上打开,它可能会 ...
- 不知道PPT转PDF简单方法有哪些?三个方法让你知道PPT转PDF怎么转
我们有时候制作了一个演示文稿,想要把它发送到其它地方进行讲解的准备,但是我们发现在传输前后,部分PPT的内容.排版等会出现变化,这个时候就有朋友想知道,有什么办法可以避免这种情况的发生吗?其实是有的, ...
- excel转换pdf java_用Java把Excel转换成PDF的方法
用Java把Excel转换成PDF的方法 发布时间:2020-05-30 11:48:45 来源:亿速云 阅读:216 作者:鸽子 Java 将Excel转为PDF 本文将介绍在Java程序中如何将E ...
- 转https_免费!这个PPT转PDF的方法不学可惜!
职场人士对PPT演示文档都不陌生,尤其是商务.营销团队利用PPT向客户推荐产品.推广计划更是家常便饭.但是作为一个合格的商务营销人员,汇报.方案只保存PPT一种格式是远远不够的,因为经常在不同产地演示 ...
- java word,excel,ppt转pdf
准备工作 1.下载 jacob.jar 链接:https://pan.baidu.com/s/1TWIGyX9A3xQ6AG9Y3mVlVg 提取码:abcd 2.下载安装wps WPS Offi ...
- Java实现word、excel、ppt转pdf文件,pdf转图片(无水印)
在网上也是找了好久才找到的一些比较好的资料,我自己总结梳理了一下,方便后面各位小伙伴使用. 1.效果图 所需的架包百度网盘: 百度链接https://pan.baidu.com/s/1oGsL7hSo ...
- Java 批量导出PPT为图片,并合并生成PDF
Java 批量导出PPT为图片,并合并生成PDF 最近有小伙伴需要把PPT打印,但苦于PPT太多,浪费纸张,所以特别写了这个合并多张PPT到一张A4纸上并生成PDF方便打印的程序: 前期准备 把PPT ...
- ppt转pdf如何质量无损?这俩种方法值得收藏
文件在被转换的时候我们最担心的莫过于转换的过程中出现了问题,不仅导致源文件受损更是转换完成后的文件无法使用,那么就像感冒一样,我们不要等已经感冒再吃药,应当先预防感冒,下面分享俩种ppt转pdf的方法 ...
- python多层遍历批量转换PPT为PDF
Python多层遍历转换PPT为PDF 1. 需求分析 2. 环境配置 3. PPT转为PDF 4.文件夹遍历 5. 总程序 参考资料 1. 需求分析 因为Mac制作的PPT在一些Windows电脑打 ...
最新文章
- vue中一个组件导入另一个组件
- 科创板首发过会,格灵深瞳“三变”交出IPO答卷
- Windows查杀端口
- Python函数的递归调用
- 泛函分析3——线性空间和赋范线性空间总结
- 关于在页面中针对不同版本的IE浏览器实现不同的JS或者CSS样式
- 案例|自建or现成工具?小型创业团队敏捷研发探索
- 可爱的朋友,你是否有很多问号
- php mysql 读取数据_PHP MySQL 读取数据
- Jmeter在Windows上分布式压测遇到的坑
- edius隐藏快捷键_EDIUS素材隐藏快捷键是什么啊?
- 直观理解图片的EXIF orientation
- AUTOSAR MCAL解析:Wdg
- OpenCV人脸目标检测、摄像头实时检测
- 新启动计算机顺序,最新版本:计算机启动启动项设置
- 沙龙与华为强强携手,机甲龙惊艳广州车展
- 十分钟实现灭霸打响指灰飞烟灭的效果
- Flowerpot(单调队列)
- 腾达u6无线网卡 linux,腾达U6驱动下载 腾达U6无线网卡驱动 v0.12.2.3 官方版 下载-脚本之家...
- SpringMVC使用form标签提交表单信息出现Invalid property 'department' of bean class的问题及解决办法