java ppt转pdf
依赖
<!-- 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>
代码工具
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 org.apache.poi.xslf.usermodel.*;
import org.apache.poi.hslf.usermodel.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.*;import cn.hutool.core.util.StrUtil;import com.itextpdf.text.Image;public class PptToPdf {public static void main(String[] args) {pptx2Pdf("C:\\Users\\admin\\Desktop\\空白演示.pptx","E:\\a\\excel转pdf\\1113.pdf");}public static boolean pptx2Pdf(String pptPath, String pdfPath) {if (StrUtil.isEmpty(pptPath)) {throw new RuntimeException("文档路径不能为空");}// 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 (StrUtil.isEmpty(pptPath)) {throw new RuntimeException("word文档路径不能为空");}if (StrUtil.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;}}
controller测试代码
@PostMapping("/pptx2Pdf")public String pptx2Pdf(MultipartFile file ) throws IllegalStateException, IOException{String fileName = file.getOriginalFilename();String localFilePath = StrUtil.appendIfMissing("/data/test", "/") + fileName;file.transferTo(new File(localFilePath));PptToPdf.pptx2Pdf(localFilePath, "/data/test/" + fileName.split("\\.")[0] + ".pdf");return "success可喜可贺";}
java ppt转pdf相关推荐
- Java PPT转PDF 亲测无水印
最近有个需求需要把PPT文件转为PDF,整了好久转换完成一直都是有水印的,最终终于找到了去水印的办法,记录一下. jar包自取 链接:https://pan.baidu.com/s/1IBJGQcnu ...
- java ppt 转图片格式_Java PPT(X)转图片、PDF和SVG
(一)简介: 工作中,PowerPoint文档有时需要被转换为PDF/图像文件来存档.因为PDF或图片的页面布局是固定的,很难被修改且能被大多数设备打开,所以PDF或者图片比起PowerPoint格式 ...
- java ppt转html_word,ppt,excel转pdf,pdf转html工具类搭建
我看到很多需求要求word,excel,ppt,pptx转pdf等工具类.还有就是pdf转图片转html这里介绍一个这个工具类. 引入pom.xml com.aspose aspose-pdf 11. ...
- 【Java编程系列】java用POI、Itext生成并下载PPT、PDF文件
热门系列: [Java编程系列]WebService的使用 [Java编程系列]在Spring MVC中使用工具类调用Service层时,Service类为null如何解决 [Java编程系列]Spr ...
- java对word、Excel、PPT、PDF文件加密
java对word.Excel.PPT.PDF文件加密 所需依赖:Spire.Office.jar 说明:该解决方案使用spire.office(免费版)该版本有使用限制谨慎使用对于小文件来说足够了 ...
- nodejs调用java的jar包进行PPT转pdf
nodejs调用java的jar包进行PPT转pdf 参考我之前写的"java版ppt转pdf(需要用到openoffice)linux版本"得到jar包:取名Test.jar n ...
- JAVA实现把PPT转PDF的方法
文章目录 前言 一.Apache poi是什么? 二.具体实现 1. 引入依赖 2. ppt/pptx转换pdf (返回InputStream) 3. ppt/pptx转换pdf (返回pdf文件) ...
- aspose使用合集java(Word、Excel、PPT转PDF)
aspose使用合集java(Word.Excel.PPT转PDF) aspose使用合集java(Word.Excel.PPT转PDF 文档所需jar包 Word转为PDF 获取license 简单 ...
- Java实现word、excel、ppt转pdf文件,pdf转图片(无水印)
在网上也是找了好久才找到的一些比较好的资料,我自己总结梳理了一下,方便后面各位小伙伴使用. 1.效果图 所需的架包百度网盘: 百度链接https://pan.baidu.com/s/1oGsL7hSo ...
最新文章
- webApp开发-功能模块开发流程
- local_irq_disable
- java中子类和父类的初始化和函数调用关系
- 盘点2020年10个最难忘的数据泄露事件
- 你应该如何选择笔记软件?
- SAP Event Mesh 简介
- html select 修改默认箭头样式,自定义select标签箭头样式
- 利用CloudIDE当做简单linux环境,执行简单python工程任务
- 沈阳大学生招聘2020计算机,2020沈阳市高校毕业生基层公共岗位服务计划人员招录600人...
- Could not find artifact org.olap4j:olap4j:pom:0.9.7.309-JS-3 in alimaven
- 使用半透明的DIV实现禁用页面功能
- 程序设计中为什么要解耦?
- Hexo报错Usage: hexo command处理及图片显示问题
- 05-Vue报错 Uncaught SyntaxError: Identifier has already been declared和路由
- Linux环境下安装MySQL 5.6.45
- php 富文本编辑器,曾经用过的十大富文本编辑器
- 帮忙framebuffer扫盲
- css样式给标签加上小手图标
- MATLAB(1)MATLAB工作环境
- POI读取Excel时报错java.util.zip.ZipException: invalid stored block lengths
热门文章
- GoDaddy域名添加不带www的域名CNAME记录
- 基于单片机的音乐倒数计数器
- 126届127届128届广交会名录免费送
- QT 如何获取主窗口的指针
- 六年级有甲乙丙三个班c语言,黄瓜怎么做好吃C语言逻辑推理例题(附答案)吝啬的近义词...
- 二维图画法入门_CAD二维零件图画法教程
- KFC简易点餐收银模拟系统
- WindowsXP解决“您可能是盗版软件的受害者”问题
- JAVA基础--自定义异常exception
- 我的世界1.8.1java下载_我的世界1.8.1电脑版