word、excel、ppt转为PDF

相关引用对象在代码里了

相关依赖

<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.0.1</version></dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.0.1</version>
</dependency>
<!-- word转pdf依赖包 -->
<dependency><groupId>com.documents4j</groupId><artifactId>documents4j-local</artifactId><version>1.0.3</version>
</dependency>
<dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-word</artifactId><version>1.0.3</version>
</dependency>
<!-- ppt转pdf,excel转pdf需要用5.2.0以上版本 -->
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.2.0</version></dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.2</version>
</dependency>
<!-- excel转pdf字体使用 -->
<dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version>
</dependency>

word转pdf

	import com.documents4j.api.DocumentType;import com.documents4j.api.IConverter;import com.documents4j.job.LocalConverter;import java.io.*;/*** 实现word转pdf** @param sourcePath 源文件地址 如 D:\\1.doc* @param targetPath 目标文件地址 如 D:\\1.pdf*/public static void wordToPdf(String sourcePath, String targetPath) {File inputWord = new File(sourcePath);File outputFile = new File(targetPath);try {InputStream docxInputStream = new FileInputStream(inputWord);OutputStream outputStream = new FileOutputStream(outputFile);IConverter converter = LocalConverter.builder().build();converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).schedule().get();outputStream.close();docxInputStream.close();log.info("转换完毕 targetPath = {}", outputFile.getAbsolutePath()+",targetPath = " + outputFile.getAbsolutePath());converter.shutDown();} catch (Exception e) {log.error("word转pdf失败:"+e.getMessage(), e);}}

ppt转pdf

	import com.itextpdf.text.*;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;import org.apache.poi.xslf.usermodel.*;import java.awt.*;import java.awt.Color;import java.awt.image.BufferedImage;import java.io.*;/*** ppt转为pdf* @param sourcePath 源文件地址 如 D:\\1.pptx* @param targetPath 目标文件地址 如 D:\\1.pdf*/public static void pptToPdf(String sourcePath,String targetPath) {Document document = null;XMLSlideShow slideShow = null;FileOutputStream fileOutputStream = null;PdfWriter pdfWriter = null;ByteArrayOutputStream baos = null;byte[] resBytes = null;try {baos=new ByteArrayOutputStream();//使用输入流pptx文件slideShow = new XMLSlideShow(new FileInputStream(new File(sourcePath)));//获取幻灯片的尺寸Dimension dimension = slideShow.getPageSize();//创建一个写内容的容器document = new Document(PageSize.A4, 72, 72, 72, 72);//使用输出流写入pdfWriter = PdfWriter.getInstance(document, baos);//使用之前必须打开document.open();pdfWriter.open();PdfPTable pdfPTable = new PdfPTable(1);//获取幻灯片java.util.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对象中com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(bufferedImage, null);image.scalePercent(50f);// 写入单元格pdfPTable.addCell(new PdfPCell(image, true));document.add(image);}document.close();pdfWriter.close();resBytes = baos.toByteArray();FileOutputStream outputStream = new FileOutputStream(targetPath);outputStream.write(resBytes);outputStream.flush();outputStream.close();} catch (Exception e) {log.error("pptx转pdf异常:"+e.getMessage(),e);} finally {try {if (baos != null) {baos.close();}} catch (Exception e) {log.error("pptx转pdf关闭io流异常:"+e.getMessage(),e);}}}

excel转pdf

	import com.itextpdf.text.*;import com.itextpdf.text.Font;import com.itextpdf.text.pdf.BaseFont;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;import lombok.SneakyThrows;import org.apache.poi.ss.usermodel.*;    /**** @param sourcePath 源文件地址 如 D:\\1.xlsx* @param targetPath 目标文件地址 如 D:\\1.pdf*/@SneakyThrowsprivate void excelToPdf(String sourcePath, String targetPath ){try (Workbook workbook = WorkbookFactory.create(new File(sourcePath));FileOutputStream fos = new FileOutputStream(targetPath )) {// 获取第一个工作表Sheet sheet = workbook.getSheetAt(0);// 创建PDF文档对象Document document = new Document(PageSize.A4, 50, 50, 50, 50);// 创建PDF输出流PdfWriter writer = PdfWriter.getInstance(document, fos);// 打开PDF文档document.open();// 创建PDF表格对象PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());// 设置表格宽度table.setWidthPercentage(100);// 设置表格标题,Example Table为表格标题Paragraph title = new Paragraph("Example Table", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));title.setAlignment(Element.ALIGN_CENTER);document.add(title);// 添加表格内容for (Row row : sheet) {for (Cell cell : row) {PdfPCell pdfCell = new PdfPCell(new Paragraph(cell.getStringCellValue(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));pdfCell.setBorderWidth(1f);pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);table.addCell(pdfCell);}}// 添加表格到PDF文档document.add(table);// 关闭PDF文档document.close();} catch (IOException | DocumentException e) {e.printStackTrace();}}

相关文件参考:
word转pdf : https://gitee.com/wu_ze_wen/word_trans_pdf?_from=gitee_search
ppt转pdf : https://blog.csdn.net/qq_30436011/article/details/127737553?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-127737553-blog-127973320.235%5Ev38%5Epc_relevant_anti_t3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-127737553-blog-127973320.235%5Ev38%5Epc_relevant_anti_t3&utm_relevant_index=4

excel转pdf :
https://blog.csdn.net/sqL520lT/article/details/131430166
https://www.cnblogs.com/iyyy/p/9346935.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/85360.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

好用的记笔记app选哪个?

当你在日常生活中突然获得了一个灵感&#xff0c;或者需要记录会议的重要内容&#xff0c;或者是学校课堂上的笔记&#xff0c;你通常会拿出手机&#xff0c;因为它总是在你身边&#xff0c;随时可用。这时候&#xff0c;一款好的记笔记App可以让你事半功倍。 敬业签是一款全面…

「UG/NX」Block UI 从列表选择部件SelectPartFromList

✨博客主页何曾参静谧的博客📌文章专栏「UG/NX」BlockUI集合📚全部专栏「UG/NX」NX二次开发「UG/NX」BlockUI集合「VS」Visual Studio「QT」QT5程序设计「C/C+&#

Qt5开发及实例V2.0-第十五章-Qt单元测试框架

Qt5开发及实例V2.0-第十五章-Qt单元测试框架 第15章 Qt 5单元测试框架15.1 QTestLib框架15.2 简单的Qt单元测试15.3 数据驱动测试15.4 简单性能测试 本章相关例程源码下载1.Qt5开发及实例_CH1501.rar 下载2.Qt5开发及实例_CH1502.rar 下载3.Qt5开发及实例_CH1503.rar 下载4.Qt5…

构建个人云存储:本地电脑搭建SFTP服务器,开启公网访问,轻松共享与管理个人文件!

本地电脑搭建SFTP服务器&#xff0c;并实现公网访问 文章目录 本地电脑搭建SFTP服务器&#xff0c;并实现公网访问1. 搭建SFTP服务器1.1 下载 freesshd 服务器软件1.3 启动SFTP服务1.4 添加用户1.5 保存所有配置 2. 安装SFTP客户端FileZilla测试2.1 配置一个本地SFTP站点2.2 内…

stm32之GPIO库函数点灯分析

stm32官方为了方便开发者&#xff0c;利用CubeMX 生成HAL库有关的C代码。HAL库就是硬件抽象层(hardware abstraction layer)&#xff0c;生成一系列的函数帮助我们快速生成工程&#xff0c;脱离复杂的寄存器配置。stm32相对于51来功能强大&#xff0c;但是寄存器的数量也不是一…

C++核心编程——P22-练习案例2:点和圆的关系

在一个类中可以让另一个类作为这个类的成员 #include<iostream> using namespace std; class Point//点类 { public:void setx(int x){c_x x;}int getx(){return c_x;}void sety(int y){c_y y;}int gety(){return c_y;}//建议将属性设置为私有&#xff0c;对外提供接口…

Django(20):信号机制

目录 信号的工作机制信号的应用场景两个简单例子Django常用内置信号如何放置信号监听函数代码自定义信号第一步&#xff1a;自定义信号第二步&#xff1a;触发信号第三步&#xff1a;将监听函数与信号相关联 信号的工作机制 Django 框架包含了一个信号机制&#xff0c;它允许若…

pcl--第十二节 2D和3D融合和手眼标定

2D&3D融合 概述 截止目前为止&#xff0c;我们学习了机器人学&#xff0c;学习了2D和3D视觉算法。我们也学习了2D相机(图像数据的来源)和3D相机(点云数据的来源)工作原理。 实际上&#xff0c;我们最终要做的&#xff0c;是一个手眼机器人系统。在这个系统里&#xff0c…

pytorch学习------常见的优化算法

优化算法 优化算法就是一种调整模型参数更新的策略&#xff0c;在深度学习和机器学习中&#xff0c;我们常常通过修改参数使得损失函数最小化或最大化。 优化算法介绍 1、梯度下降算法&#xff08;batch gradient descent BGD&#xff09; 每次迭代都需要把所有样本都送入&…

宝塔composer 安装laravel依赖出现的问题

环境宝塔、PHP版本8.0.2、laravel9 问题1&#xff1a;PHP Fatal error: Uncaught Error: Call to undefined function Composer\XdebugHandler\putenv() 办法&#xff1a;把PHP版本disable_functions这个中的putenv去掉&#xff0c;这个意思就是putenv被PHP对应的版本禁用了&…

深入了解队列数据结构:定义、特性和实际应用

文章目录 &#x1f34b;引言&#x1f34b;队列的定义&#x1f34b;队列的实现&#x1f34b;队列的应用&#x1f34b;练习题&#x1f34b;结语 &#x1f34b;引言 队列&#xff08;Queue&#xff09;是计算机科学中一种重要的数据结构&#xff0c;它常用于各种应用程序中&#x…

RabbitMQ里的几个重要概念

RabbitMQ中的一些角色&#xff1a; publisher&#xff1a;生产者consumer&#xff1a;消费者exchange个&#xff1a;交换机&#xff0c;负责消息路由&#xff0c;接受生产者发送的消息&#xff0c;把消息发送到一个或多个队列里queue&#xff1a;队列&#xff0c;存储消息virt…

Hive【Hive(一)DDL】

前置准备 需要启动 Hadoop 集群&#xff0c;因为我们 Hive 是在 Hadoop 集群之上运行的。 从DataGrip 或者其他外部终端连接 Hive 需要先打开 Hive 的 metastore 进程和 hiveserver2 进程。metastore 和 hiveserver2 进程的启动过程比较慢&#xff0c;不要着急。 Hive DDL 数据…

基于SpringBoot的网上超市系统的设计与实现

目录 前言 一、技术栈 二、系统功能介绍 管理员功能实现 用户功能实现 三、核心代码 1、登录模块 2、文件上传模块 3、代码封装 前言 网络技术和计算机技术发展至今&#xff0c;已经拥有了深厚的理论基础&#xff0c;并在现实中进行了充分运用&#xff0c;尤其是基于计…

微软在Windows 11推出Copilot,将DALL-E 3集成在Bing!

美东时间9月21日&#xff0c;微软在美国纽约曼哈顿举办产品发布会&#xff0c;生成式AI成为重要主题之一。 微软表示&#xff0c;Copilot将于9月26日在Windows 11中推出&#xff1b;Microsoft 365 Copilot 将于11 月1日向企业客户全面推出&#xff1b;将OpenAI最新的文本生成图…

1201. 丑数 III -- 巧用二分搜索

1201. 丑数 III class Solution:"""巧用二分搜索1201. 丑数 IIIhttps://leetcode.cn/problems/ugly-number-iii/"""def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:# 题目说本题结果在 [1, 2 * 10^9] 范围内left, right 1…

【货币单位换算】python实现-附ChatGPT解析

1.题目 题目描述: 记账本上记录了若干条多国货币金额,需要转换成人民币分 (fen),汇总后输出。 每行记录一条金额,金额带有货币单位,格式为数宁+单位, 可能是单独元,或者单独分,或者元与分的组合。 要求将这些货币全部换算成人民币分 (fen) 后进行汇总, 汇总结果仅保留整…

【论文阅读 08】Adaptive Anomaly Detection within Near-regular Milling Textures

2013年&#xff0c;太老了&#xff0c;先不看 比较老的一篇论文&#xff0c;近规则铣削纹理中的自适应异常检测 1 Abstract 在钢质量控制中的应用&#xff0c;我们提出了图像处理算法&#xff0c;用于无监督地检测隐藏在全局铣削模式内的异常。因此&#xff0c;我们考虑了基于…

GitHub Copilot Chat

9月21日&#xff0c;GitHub在官网宣布&#xff0c;所有个人开发者可以使用GitHub Copilot Chat。用户通过文本问答方式就能生成、检查、分析各种代码。 据悉&#xff0c;GitHub Copilot Chat是基于OpenAI的GPT-4模型打造而成&#xff0c;整体使用方法与ChatGPT类似。例如&…