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

Java实现Word文档转PDF,PDF转Word,PDF转Excel,PDF转换工具

前言

java实现word文档转PDF,PDF转word
解决只能转换4页问题
解决每页头部存在水印问题

实现

引入依赖

<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>

破解的jar包
链接: https://pan.baidu.com/s/1MO8OBuf4FQ937R9KDtofPQ 提取码: 4tsn

package com.common.util;import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;import java.io.*;/*** PDF转换工具类* @author yyq*/
public class PdfUtil {public static void main(String[] args) {//pdfToObj("C:\\Users\\Administrator\\Desktop\\测试.pdf", "docx");String filePath = "C:\\Users\\Administrator\\Desktop\\测试.docx";String outFilePath = "C:\\Users\\Administrator\\Desktop\\测试.pdf";objToPdf(filePath, outFilePath, "docx");}/*** PDF 转 doc、Excel、xml* @param pdfPath 需要转换的pdf路径* @param suffix 文件后缀*/public static void pdfToObj(String pdfPath, String suffix) {long old = System.currentTimeMillis();FileOutputStream os = null;try {// 新建一个word文档String wordPath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + "." + suffix;os = new FileOutputStream(wordPath);// doc是将要被转化的word文档Document doc = new Document(pdfPath);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换if(suffix.equals("doc")){doc.save(os, SaveFormat.Doc);}else if(suffix.equals("docx")){doc.save(os, SaveFormat.DocX);}else if(suffix.equals("xls") || suffix.equals("xlsx")){doc.save(os, SaveFormat.Excel);}else if(suffix.equals("html")){doc.save(os, SaveFormat.Html);}else if(suffix.equals("xml")){doc.save(os, SaveFormat.Xml);}os.close();// 转化耗时long now = System.currentTimeMillis();long useTime = ((now - old) / 1000);System.out.println("Pdf 转 Word 共耗时:" + useTime + "秒");} catch (Exception e) {System.out.println("Pdf 转 Word 失败...");e.printStackTrace();}finally {try {os.close();} catch (IOException e) {e.printStackTrace();}}}/*** txt、doc、Excel、xml 转 PDF* @param oidPath 需要转的文件路径* @param newPath 转成的PDF文件路径*/public static void objToPdf(String oidPath, String newPath, String suffix){InputStream inputStream = null;OutputStream outputStream = null;IConverter converter = null;try {// 源文件地址File oidFile = new File(oidPath);// 导出文件地址File newFile = new File(newPath);// 文件读取inputStream = new FileInputStream(oidFile);outputStream = new FileOutputStream(newFile);// 开始转换converter = LocalConverter.builder().build();boolean flag = false;if(suffix.equals("doc")){flag = converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();}else if(suffix.equals("docx")){flag = converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();}else if(suffix.equals("txt")){flag = converter.convert(inputStream).as(DocumentType.TEXT).to(outputStream).as(DocumentType.PDF).execute();}else if(suffix.equals("xls")){flag = converter.convert(inputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();}else if(suffix.equals("xlsx")){flag = converter.convert(inputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();}else if(suffix.equals("html")){flag = converter.convert(inputStream).as(DocumentType.MHTML).to(outputStream).as(DocumentType.PDF).execute();}else if(suffix.equals("xml")){flag = converter.convert(inputStream).as(DocumentType.XML).to(outputStream).as(DocumentType.PDF).execute();}if (flag) {converter.shutDown();}inputStream.close();outputStream.close();System.out.println("转换成功");} catch (Exception e) {converter.shutDown();e.printStackTrace();System.out.println("转换失败");}finally {try {inputStream.close();outputStream.close();} catch (IOException e) {e.printStackTrace();}}}}

源码路径:https://download.csdn.net/download/weixin_43992507/88215577

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

相关文章:

  • Docker部署ES服务,全量同步的时候内存爆炸,ES自动关闭,CPU100%
  • Python——添加照片边框
  • 《高性能MySQL》——查询性能优化(笔记)
  • 【Linux操作系统】编译过程中遇到的问题-为什么加-c?执行文件提示无法执行二进制文件?main函数参数argc和*argv[]的作用和理解?
  • 【数据结构与算法——TypeScript】图结构(Graph)
  • C语言字符串拷贝函数详解及示例代码
  • IntelliJ IDEA热部署:JRebel插件的安装与使用
  • iTOP-3568开发板使用OpenCV处理图像-颜色转换
  • Python技巧----解压序列/可迭代对象赋值给多个变量
  • 16.3.2 【Linux】程序的管理
  • Linux命令200例:date用于显示和设置系统的日期和时间
  • excel入门
  • 单模光纤模场强度分布以及高斯近似的MATLAB仿真
  • Springboot 在 redis 中使用 BloomFilter 布隆过滤器机制
  • 什么是管理的本质?
  • 02:STM32--EXTI外部中断
  • CLickhouse核心特性
  • 如何运用小程序技术闭环运营链路?
  • 使用chatGPT-4 畅聊量子物理学(二)
  • 读《Flask Web开发实战》(狼书)笔记 | 第1、2章
  • Tomcat+Http+Servlet
  • Leaflet入门,Leaflet如何实现vue双向绑定数据添加到图片标记物到地图上,动态根据vue数据更新到地图上以及鼠标经过标记物显示提示框
  • C++设计模式结构型之代理模式
  • 使用PHP实现实时聊天功能的匿名聊天与加密传输
  • Maven 基础之依赖管理、范围、传递、冲突
  • Python jupyter lab 设置
  • 水库大坝安全监测系统实施方案
  • GloVe、子词嵌入、BPE字节对编码、BERT相关知识(第十四次组会)
  • Debian10:安装PHPVirtualBox
  • RANSAC算法