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

JAVA实现将PDF转换成word文档

POM.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId>
<!--       <version>3.2.1</version>--><version>2.3.9.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.jack</groupId><artifactId>jackDemo</artifactId><version>0.0.1-SNAPSHOT</version><name>jackDemo</name><description>jackDemo</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.75</version></dependency><dependency><groupId>org.openpnp</groupId><artifactId>opencv</artifactId><version>4.5.3-4</version></dependency><!-- Apache POI for Excel files --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version> <!-- 请检查并使用最新版本 --></dependency><!-- Apache POI dependencies (these may be included automatically by Maven, but it's good to be explicit) --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.3</version> <!-- 与poi-ooxml版本保持一致 --></dependency><!-- Apache Commons Collections (required by POI) --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.4</version> <!-- 确保版本与你的项目兼容 --></dependency><!-- Apache Commons IO (optional, but useful for file handling) --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version> <!-- 确保版本与你的项目兼容 --></dependency><!-- PDFBox for reading PDF files --><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.24</version></dependency><!-- docx4j for creating Word documents --><dependency><groupId>org.docx4j</groupId><artifactId>docx4j</artifactId><version>3.2.1</version></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.1</version></dependency><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>2.3.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

java 文件:
 

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.Text;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class PdfToWordConverter {public static void main(String[] args) throws Exception{String pdfFilePath = "D:\\word\\何以为父影响彼此一生的父子关系.pdf"; // 替换为你的PDF文件路径String wordFilePath = "D:\\word\\何以为父影响彼此一生的父子关系.docx"; // 生成的Word文件路径try {// 读取PDF文件内容String pdfText = extractTextFromPdf(pdfFilePath);// 将内容写入Word文档createWordDocument(wordFilePath, pdfText);System.out.println("PDF to Word conversion completed successfully!");} catch (IOException e) {e.printStackTrace();}}public static String extractTextFromPdf(String filePath) throws IOException {PDDocument document = PDDocument.load(new FileInputStream(filePath));PDFTextStripper pdfStripper = new PDFTextStripper();return pdfStripper.getText(document);}public static void createWordDocument(String filePath, String content) throws Exception {WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();Body body = mainDocumentPart.getContents().getBody();// 将内容按段落分割并添加到Word文档中String[] paragraphs = content.split("\\r?\\n");for (String paragraph : paragraphs) {P p = new P();R r = new R();Text text = new Text();text.setParent(paragraph);r.getContent().add(text);p.getContent().add(r);body.getContent().add(p);}// 保存Word文档try (FileOutputStream out = new FileOutputStream(new File(filePath))) {wordMLPackage.save(out);}}
}

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

相关文章:

  • 前端-Git
  • 如何分析Windows防火墙日志
  • 工作坊报名|使用 TEN 与 Azure,探索你的多模态交互新场景
  • 学习笔记041——Elastic Search的学习与使用以及SpringBoot整合
  • R安装rgdal报错 解决办法
  • 【智能制造-46】人机工程(工厂自动化)
  • C#笔记(5)
  • 【软件国产化】| Windows和Linux下文件名后缀是否区分大小写
  • 讨论JAVA、JVM与Spring
  • 【04】MySQL数据库和数据表的基本操作详解与实例
  • Spring中实现动态数据源切换,基于AbstractRoutingDataSource
  • StarRocks-join优化
  • js 高亮文本中包含的关键词标红
  • DVWA靶场——File Inclusion
  • Android Framework禁止弹出当前VOLTE不可用的提示窗口
  • OceanBase 大数据量导入(obloader)
  • w058基于web的美发门店管理系统
  • 实时数据开发 | checkpoints监控和调优
  • el-tree的使用及控制全选、反选、获取选中
  • 机器学习(二十五):决策树算法以及决策树和神经网络的对比
  • 新版布谷直播软件源码开发搭建功能更新明细
  • vue3 reactive响应式实现源码
  • git的使用(简洁版)
  • 使用命令行创建 Maven 项目
  • JVM_栈详解一
  • Linux 金仓数据库安装和使用
  • STM32笔记(串口IAP升级)
  • C++网络编程:select IO多路复用及TCP服务器开发
  • 部署 L2JMobius 天堂2芙蕾雅版本
  • C#开发合集