当前位置: 首页 > news >正文

SpringBoot、JAVA中excel、rtf、doc转PDF

话不多说,直接上干货

// 官方文档的要求 无需理会public static boolean getLicense() {boolean result = false;try {String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());License aposeLic = new License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}

doc转PDF

/*** @param inputStream  源文件输入流* @param outputStream pdf文件输出流**/public static boolean doc2pdf(InputStream inputStream, OutputStream outputStream) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return false;}try {// 将源文件保存在com.aspose.words.Document中,具体的转换格式依靠里面的save方法com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换doc.save(outputStream, SaveFormat.PDF);} catch (Exception e) {e.printStackTrace();return false;}finally {if (outputStream != null) {try {outputStream.flush();outputStream.close();} catch (IOException e) {e.printStackTrace();}}}return true;}

excel转PDF

/*** @param inputStream  源文件输入流* @param outputStream pdf文件输出流**/public static boolean excelToPdf(InputStream inputStream, OutputStream outputStream) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return false;}try {com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(inputStream);// 原始excel路径com.aspose.cells.PdfSaveOptions pdfSaveOptions = new com.aspose.cells.PdfSaveOptions();pdfSaveOptions.setOnePagePerSheet(false);int[] autoDrawSheets={3};//当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。autoDraw(wb,autoDrawSheets);int[] showSheets={0};//隐藏workbook中不需要的sheet页。printSheetPage(wb,showSheets);wb.save(outputStream, pdfSaveOptions);outputStream.flush();outputStream.close();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return true;}

RTF转PDF

/*** @param inputStream  源文件输入流* @param outputStream pdf文件输出流**/public static boolean rtfToPdf(InputStream inputStream, OutputStream outputStream) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return false;}try {// 将源文件保存在com.aspose.words.Document中,具体的转换格式依靠里面的save方法com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换doc.save(outputStream, SaveFormat.PDF);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return true;}

其中依赖的方法

/*** 设置打印的sheet 自动拉伸比例* @param wb* @param page 自动拉伸的页的sheet数组*/public static void autoDraw(com.aspose.cells.Workbook wb,int[] page){if(null!=page&&page.length>0){for (int i = 0; i < page.length; i++) {wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();wb.getWorksheets().get(i).getVerticalPageBreaks().clear();}}}/*** 隐藏workbook中不需要的sheet页。** @param wb* @param page 显示页的sheet数组*/public static void printSheetPage(com.aspose.cells.Workbook wb, int[] page) {for (int i = 1; i < wb.getWorksheets().getCount(); i++) {wb.getWorksheets().get(i).setVisible(false);}if (null == page || page.length == 0) {wb.getWorksheets().get(0).setVisible(true);} else {for (int i = 0; i < page.length; i++) {wb.getWorksheets().get(i).setVisible(true);}}}

其中需要依赖一些jar包,在这里

http://www.lryc.cn/news/348039.html

相关文章:

  • 生信技能45 - 基于docker容器运行生信软件
  • 算法训练营第63天|LeetCode 84.柱状图中最大的矩形
  • python跟C++选哪个?
  • 速锐得深入解析吉利几何CAN总线数据通信网络的拓扑层级框架技术
  • 数据分析的数据模型
  • SQL注入-通达OA SQL注入漏洞【CVE-2023-4166】原理及检测思路分析
  • 数据结构(七)复杂度渐进表示
  • 3d如何同时贴两个图在模型上?---模大狮模型网
  • 【全开源】Java同城预约月嫂服务上门服务本地服务源码APP+小程序+公众号+H 5
  • 汇聚荣科技:拼多多开店时后期押金可以退吗?
  • 【机器学习与实现】K近邻算法
  • 【Python探索之旅】初识Python
  • MongoDB和AI 赋能行业应用:制造业和汽车行业
  • FileZilla一款免费开源的FTP软件,中文正式版 v3.67.0
  • 44.WEB渗透测试-信息收集-域名、指纹收集(6)
  • 【面经】Linux
  • TriCore:Interrupt 2
  • ollama api只能局域网访问,该怎么破?
  • 规则引擎drools Part5
  • API设计之争:一个接口一个职能还是一个页面所需字段?
  • 第一天复习Qt文件读取
  • 使用非官网购买Chatgpt的api调用
  • 通过 Java 操作 redis -- hash 哈希表基本命令
  • Unity 性能优化之遮挡剔除(Occlusion Culling)(六)
  • android——关于app性能优化篇
  • 单链表经典算法LeetCode--203.移除链表元素(两种方法解)
  • MySQL—子查询
  • ffmpeg 读取流报错: Non-monotonous DTS in output stream
  • yo!这里是socket网络编程相关介绍
  • polars学习-03 数据类型转换