第一步将jar导入mvn库
下载地址 0积分
https://download.csdn.net/download/qq_35908944/18549670

mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=18.8 -Dpackaging=jar -Dfile=E:/wordpdf/aspose-words-18.6-jdk16.jar
mvn install:install-file -DgroupId=com.itextpdf -DartifactId=itextpdf-itextpdf -Dversion=5.5.6 -Dpackaging=jar -Dfile=E:/wordpdf/itextpdf-5.5.6.jar
mvn install:install-file -DgroupId=com.aspose.slides -DartifactId=aspose-slides -Dversion=15.9.0 -Dpackaging=jar -Dfile=E:/wordpdf/aspose.slides-15.9.0.jar
mvn install:install-file -DgroupId=com.cells -DartifactId=aspose-cells -Dversion=8.5.2 -Dpackaging=jar -Dfile=E:/wordpdf/aspose-cells-8.5.2.jar
mvn install:install-file -DgroupId=com.itext -DartifactId=itext-asian -Dversion=5.2.0 -Dpackaging=jar -Dfile=E:/wordpdf/itext-asian-5.2.0.jar
mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=18.8 -Dpackaging=jar -Dfile=E:/wordpdf/aspose-words-15.8.0-jdk16.jar

<!-- word转pdf --><dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>18.8</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.6</version></dependency><dependency><groupId>com.aspose.slides</groupId><artifactId>aspose-slides</artifactId><version>15.9.0</version></dependency><dependency><groupId>com.cells</groupId><artifactId>aspose-cells</artifactId><version>8.5.2</version></dependency><dependency><groupId>com.itext</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

上代码

import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;/*** @author Administrator* @USER:小清新* @date: 2021年05月08日 16:02* @PROJECT_NAME: CGCP**/public class PdfUtil {public  File Pdf(ArrayList<String> imageUrllist,String mOutputPdfFileName) {//Document doc = new Document(PageSize.A4, 20, 20, 20, 20); // new一个pdf文档com.itextpdf.text.Document doc = new com.itextpdf.text.Document();try {// pdf写入PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));// 打开文档doc.open();// 循环图片List,将图片加入到pdf中for (int i = 0; i < imageUrllist.size(); i++) {// 在pdf创建一页doc.newPage();// 通过文件路径获取imageImage png1 = Image.getInstance(imageUrllist.get(i));float heigth = png1.getHeight();float width = png1.getWidth();int percent = getPercent2(heigth, width);png1.setAlignment(Image.MIDDLE);// 表示是原来图像的比例;png1.scalePercent(percent + 3);doc.add(png1);}doc.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}File mOutputPdfFile = new File(mOutputPdfFileName); // 输出流if (!mOutputPdfFile.exists()) {mOutputPdfFile.deleteOnExit();return null;}return mOutputPdfFile; // 反回文件输出流}public  int getPercent(float h, float w) {int p = 0;float p2 = 0.0f;if (h > w) {p2 = 297 / h * 100;} else {p2 = 210 / w * 100;}p = Math.round(p2);return p;}public  int getPercent2(float h, float w) {int p = 0;float p2 = 0.0f;p2 = 530 / w * 100;p = Math.round(p2);return p;}/*** 图片文件转PDF* @param filepath* @param request* @return*/public static String imgOfPdf(String filepath, HttpServletRequest request) {boolean result = false;String pdfUrl = "";String fileUrl = "";try {// 图片list集合ArrayList<String> imageUrllist = new ArrayList<String>();imageUrllist.add(request.getSession().getServletContext()// 添加图片文件路径.getRealPath(File.separator + filepath));String fles = filepath.substring(0, filepath.lastIndexOf("."));// 输出pdf文件路径pdfUrl = request.getSession().getServletContext().getRealPath(File.separator +fles + ".pdf");fileUrl =fles+".pdf";result = true;// 生成pdfif (result) {PdfUtil pdfUtil=new PdfUtil();File file = pdfUtil.Pdf(imageUrllist, pdfUrl);file.createNewFile();}} catch (IOException e) {e.printStackTrace();}return fileUrl;}/*public static void doc2pdf(String Address, String outPath) {if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生return;}try {long old = System.currentTimeMillis();File file = new File(outPath); // 新建一个空白pdf文档FileOutputStream os = new FileOutputStream(file);Document doc = new Document(Address); // Address是将要被转化的word文档doc.save(os,SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML,// OpenDocument, PDF, EPUB, XPS, SWF// 相互转换long now = System.currentTimeMillis();System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时os.close();} catch (Exception e) {e.printStackTrace();}}*/public  boolean getLicense() {boolean result = false;try {InputStream is =this.getClass().getClassLoader().getResourceAsStream("license.xml");License aposeLic = new License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}public  String docOfPdf(String filePath, HttpServletRequest request) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return "PDF格式转化失败";}try {long old = System.currentTimeMillis();String filePath1 = filePath;String filePath2 = filePath.substring(0, filePath.lastIndexOf("."));String pdfPathString = filePath2+".pdf";// 输出pdf文件路径// 新建一个空白pdf文档File file = new File(filePath2+".pdf");FileOutputStream os = new FileOutputStream(file);// Address是将要被转化的word文档Document doc = new Document(filePath1);// 全面支持DOC, DOCX, OOXML, RTF HTML,doc.save(os, SaveFormat.PDF);// OpenDocument, PDF, EPUB, XPS, SWF// 相互转换long now = System.currentTimeMillis();// 转化用时System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");os.close();return pdfPathString;} catch (Exception e) {e.printStackTrace();}return "PDF格式转化失败";}public static boolean getLicense1() {boolean result = false;try {// license.xml应放在..\WebRoot\WEB-INF\classes路径下InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");com.aspose.cells.License aposeLic = new com.aspose.cells.License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}public  String exceOfPdf(String filePath, HttpServletRequest request) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense1()) {return "PDF格式转化失败";}try {//long old = System.currentTimeMillis();//获取路径参数String filePath1 = filePath;String filePath2 = filePath.substring(0, filePath.lastIndexOf("."));String pdfSPath = filePath2+".pdf";// 输出pdf文件路径filePath2 = filePath2+".pdf";//文件操作// 新建一个空白pdf文档File file = new File(filePath2);FileOutputStream os = new FileOutputStream(file);// 原始excel路径Workbook wb = new Workbook(filePath1);FileOutputStream fileOS = new FileOutputStream(file);wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);fileOS.close();// long now = System.currentTimeMillis();return pdfSPath;} catch (Exception e) {e.printStackTrace();}return "PDF格式转化失败";}public  boolean getLicense2() {boolean result = false;try {// license.xml应放在..\WebRoot\WEB-INF\classes路径下InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");com.aspose.slides.License aposeLic = new com.aspose.slides.License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}public  String pptUpdatePdf(String filePath, HttpServletRequest request){// 验证Licenseif (!getLicense2()) {return "PDF格式转化失败";}try {long old = System.currentTimeMillis();//File file = new File("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/pdf1.pdf");// 输出pdf路径//com.aspose.slides.Presentation pres = new  com.aspose.slides.Presentation(Address);//输入pdf路径String filePath1 = filePath;String filePath2 = filePath.substring(0, filePath.lastIndexOf("."));String pdfPathString  = filePath2 + ".pdf";// 输出pdf文件路径
//            filePath2 = request.getSession().getServletContext()
//                    .getRealPath(File.separator  + filePath2 + ".pdf");//文件操作// 新建一个空白pdf文档File file = new File(filePath2+".pdf");//输入pdf路径Presentation pres = new  Presentation(filePath1);FileOutputStream fileOS = new FileOutputStream(file);pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);fileOS.close();long now = System.currentTimeMillis();//转化过程耗时System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + file.getPath());return pdfPathString;} catch (Exception e) {e.printStackTrace();}return "PDF格式转化失败";}
}

java word excel ppt 图片转pdf相关推荐

  1. java word,excel,ppt转pdf

    准备工作 1.下载 jacob.jar  链接:https://pan.baidu.com/s/1TWIGyX9A3xQ6AG9Y3mVlVg  提取码:abcd 2.下载安装wps WPS Offi ...

  2. 用C#实现实时监测文件夹,把word/excel/ppt自动转为pdf

    很久没更新了,前段时间在准备软考,最近也忙着单位新系统的开发.所以也就没更新了,等后面几个项目做好了,我在项目分享出来!! 由于新开发的信息系统,原来旧版的信息系统,在查看文件时,都需要下载到本地,然 ...

  3. php word/excel/ppt 转pdf

    转载至:https://blog.csdn.net/sangjinchao/article/details/78053545 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, ...

  4. Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法)

    Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法) 1.需求背景 2.环境准备工作 2.1 JACOB介绍及安装 2.2 Microsoft Office W ...

  5. php word excel转pdf文件怎么打开,php office文件(word/excel/ppt)转pdf文件,pptpdf

    php office文件(word/excel/ppt)转pdf文件,pptpdf 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, 关于预览office文件实现核心就是,把o ...

  6. vba 保存word里面的图片_笔记7 【office精华课】一套课程学会Word+Excel+PPT(一)【Word】(2020年第37周 周五)...

    [office精华课] <一套课程学会Word+Excel+PPT> 课程目录:(总时长合计:28:56:25) =================================== [ ...

  7. Python办公自动化(八)|使用Python转换PDF,Word/Excel/PPT/md/HTML都能转

    Word转PDF Word转PDF应该是最常见的需求了,毕竟使用PDF格式可以更方便展示文档,虽然在Word中可以直接导出为PDF格式,但是使用Python可以批量转换,更加高效. 目前在Python ...

  8. 文末福利|使用Python转换PDF,Word/Excel/PPT/md/HTML都能转!

    往期精选 Python办公自动化|从Word到Excel Python办公自动化|从Excel到Word Python办公自动化|对比文件,光速完成 Python办公自动化|Excel表格,自动更新 ...

  9. 使用Python转换PDF,Word/Excel/PPT/md/HTML都能转!

    同一个操作执行两次,就要考虑自动化! 大家好,又到了Python办公自动化专题. 今天讲的是各位一定会接触到的PDF转换,关于各种格式的文件转换为PDF有很多第三方工具与网站可以实现,但是使用Pyth ...

最新文章

  1. selenium--字符串/整型问题Can't convert 'int' object to str implicitly提示解决方法
  2. 大型企业云化2.0的深度思考与展望
  3. python编程基础人民邮电出版社_Python编程基础与应用
  4. 计算机动画分为关键帧动画和,一个最简单的动画最少有几个关键帧
  5. rocketmq 初探(二)
  6. 【Python CheckiO 题解】Count Consecutive Summers
  7. C++Builder编程中动态更改自定义打印纸张
  8. Hystrix面试 - 深入 Hystrix 执行时内部原理
  9. django设置models.Model数据可以为空
  10. 用户体验设计答疑对话(半吊子和纯外行
  11. 广度优先搜索算法(Breath-first Search)是如何搜索一张图的?
  12. auxiliary variable(辅助变量)的引入
  13. 练习题︱ python 协同过滤ALS模型实现:商品推荐 + 用户人群放大
  14. 什么是网络基础设施?
  15. jvm垃圾回收机制查看及说明
  16. SCAU 1038 打印图案
  17. 北理计算机优营会被鸽吗,被放鸽子以后~
  18. Python骚操作 | 还原已撤回的微信消息
  19. 苏宁易购按关键字搜索suning商品 API 返回值说明
  20. 小红书KOL营销策略,先搞清楚小红书kol分类有哪些

热门文章

  1. ultraedit显示函数列表的步骤
  2. uni-app.08.自定义扩展图标
  3. 笔记本win7系统鼠标总是乱跳解决方法
  4. 768. 忽略大小写比较字符串大小(DAY19)
  5. 光是会受到磁场的影响
  6. 2021年web前端面试集锦
  7. Vue面试高频考题解析
  8. HDU - 1029 Ignatius and the Princess IV
  9. 顺丰科技视频面试 -Java研发
  10. 安全工程师最详细学习和职业规划路线