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

PDF 转Word 开源库

1. Apache PDFBox

Apache PDFBox 是一个开源的 Java 库,用于创建和操作 PDF 文档。虽然 PDFBox 本身没有直接支持 PDF 转 Word 的功能,但它可以提取 PDF 内容,你可以结合其他方法将这些内容写入 Word。

添加依赖

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.24</version> <!-- 检查是否有最新版本 -->
</dependency>

使用示例

你可以使用 PDFBox 提取文本,然后使用 Apache POI 库将其写入 Word 文档。

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class PdfToWordConverter {public static void main(String[] args) {String pdfFilePath = "path/to/input.pdf";String wordFilePath = "path/to/output.docx";try {convertPdfToWord(pdfFilePath, wordFilePath);System.out.println("PDF converted to Word successfully!");} catch (IOException e) {e.printStackTrace();}}public static void convertPdfToWord(String pdfFilePath, String wordFilePath) throws IOException {PDDocument document = PDDocument.load(new FileInputStream(pdfFilePath));PDFTextStripper stripper = new PDFTextStripper();String pdfText = stripper.getText(document);document.close();// 创建 Word 文档并写入文本XWPFDocument wordDocument = new XWPFDocument();XWPFParagraph paragraph = wordDocument.createParagraph();paragraph.createRun().setText(pdfText);try (FileOutputStream out = new FileOutputStream(wordFilePath)) {wordDocument.write(out);}wordDocument.close();}
}
http://www.lryc.cn/news/426438.html

相关文章:

  • Docker - 深入理解Dockerfile中的 RUN, CMD 和 ENTRYPOINT
  • Python 函数式编程 内置高阶函数及周边【进阶篇 3】推荐
  • 【Rust光年纪】探秘Rust GUI库:从安装配置到API概览
  • Element plus部分组件样式覆盖记录
  • 重塑业务生态,Vatee万腾平台:引领行业变革的新引擎
  • 标准术语和定义中的【架构】应该如何描述
  • 华为鸿蒙Core Vision Kit 骨骼检测技术
  • Table API SQL系统(内置)函数System (Built-in) Function详解
  • 一键运行RocketMQ5.3和Dashboard
  • HAL STM32 SG90舵机驱动控制
  • 【Kubernetes】k8s集群图形化管理工具之rancher
  • AI编程系列一1小时完成链家房价爬虫程序
  • 【JavaEE初阶】文件内容的读写—数据流
  • Spring Boot项目中使用Sharding-JDBC实现读写分离
  • 【网络安全】SSO登录过程实现账户接管
  • Admin.NET源码学习(3:LazyCaptcha使用浅析)
  • 在原生未启用kdump的BCLinux 8系列服务器上启用kdump及报错处理
  • Android架构组件中的MVVM
  • 走向绿色:能源新选择,未来更美好
  • 鸿蒙装饰器的介绍
  • 零基础5分钟上手亚马逊云科技核心云架构知识 - 权限管理最佳实践
  • [数据库][知识]SQL Server、MySQL 和 Oracle 的默认端口和数据库链接
  • 【Unity教程】使用 Animation Rigging实现IK制作程序化的动画
  • OBS混音器(Mixers)的重要性和配置指南
  • Ubuntu安装Anaconda3
  • 数据类型解码:INT、VARCHAR、DATETIME的深度解析与实践
  • 基于单片机的智能晾衣系统设计
  • Python实战项目:天气数据爬取+数据可视化(完整代码)
  • 知识改变命运 数据结构【链表面试题】
  • 计算机毕业设计 医院问诊系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试