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

长沙网站建设建网站模板免费下载

长沙网站建设建,网站模板免费下载,郑州网站制作多少钱,做php网站文章目录 方案一方案二使用说明 以下是使用 Java 实现批量 PDF 转 Word 的两种解决方案,根据需求选择适合的方式: 方案一 使用 Apache PDFBox Apache POI (仅提取文本) 适用场景:仅需提取文本内容(不支持图片/表格/格式保留&…

文章目录

  • 方案一
  • 方案二
  • 使用说明

以下是使用 Java 实现批量 PDF 转 Word 的两种解决方案,根据需求选择适合的方式:

方案一

使用 Apache PDFBox + Apache POI (仅提取文本)
适用场景:仅需提取文本内容(不支持图片/表格/格式保留)

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.*;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PdfToWordConverter {  public static void main(String[] args) {       String inputDir = "path/to/pdf/folder";        String outputDir = "path/to/word/folder";               try {                                         Files.createDirectories(Paths.get(outputDir));          File[] pdfFiles = new File(inputDir).listFiles((dir, name) -> name.endsWith(".pdf"));                        if (pdfFiles != null) {                for (File pdfFile : pdfFiles) {                   String wordPath = outputDir + File.separator +      pdfFile.getName().replace(".pdf", ".docx");                    convertPdfToWord(pdfFile.getAbsolutePath(), wordPath);                   System.out.println("转换成功: " + pdfFile.getName());               }            }       } catch (Exception e) {            e.printStackTrace();        }   }    
private static void convertPdfToWord(String pdfPath, String wordPath) throws Exception {        try (PDDocument pdfDoc = PDDocument.load(new File(pdfPath));            XWPFDocument wordDoc = new XWPFDocument()) {                        PDFTextStripper stripper = new PDFTextStripper();            String text = stripper.getText(pdfDoc);                        // 将文本写入Word           try (FileOutputStream out = new FileOutputStream(wordPath)) {               XWPFParagraph paragraph = wordDoc.createParagraph();                XWPFRun run = paragraph.createRun();       run.setText(text);                wordDoc.write(out);            }        }    }}

方案二

调用 LibreOffice 命令行 (推荐,保留格式)
适用场景:需要保留原始格式、图片、表格等(需安装 LibreOffice)

  1. 安装 LibreOffice
  • Windows:下载安装包 https://www.libreoffice.org/
  • macOS: brew install libreoffice
  • Linux: sudo apt install libreoffice
  1. Java 代码
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PdfToWordLibreOffice {    public static void main(String[] args) {       String inputDir = "path/to/pdf/folder";        String outputDir = "path/to/word/folder";                try {            Files.createDirectories(Paths.get(outputDir));            File[] pdfFiles = new File(inputDir).listFiles((dir, name) -> name.endsWith(".pdf"));                      if (pdfFiles != null) {               for (File pdfFile : pdfFiles) {                    convertWithLibreOffice(pdfFile.getAbsolutePath(), outputDir);                    System.out.println("转换成功: " + pdfFile.getName());                }           }        } catch (Exception e) {            e.printStackTrace();       }   }    private static void convertWithLibreOffice(String pdfPath, String outputDir) throws IOException, InterruptedException {      // 根据系统调整命令路径       
String     command;
if(System.getProperty("os.name").toLowerCase().contains("win")) {            
command = "\"C:\\Program Files\\LibreOffice\\program\\soffice.exe\" --convert-to docx --outdir ";       } else {           command = "libreoffice --headless --convert-to docx:MS Word 2007 XML --outdir ";        }               ProcessBuilder builder = new ProcessBuilder();       builder.command(command + "\"" + outputDir + "\" \"" + pdfPath + "\"");       
builder.directory(newFile(System.getProperty("user.home")));                
Process process = builder.start();       int exitCode = process.waitFor();               if (exitCode != 0) {            throw new IOException("转换失败,退出码: " + exitCode);       }    }}

使用说明

  1. 方案一(PDFBox+POI)
  • 优点:纯 Java 实现,无需外部依赖
  • 缺点:仅提取文本,丢失所有格式
  • 依赖(Maven):
<dependencies>         <dependency>                     <groupId>org.apache.pdfbox</groupId>             <artifactId>pdfbox</artifactId>             <version>2.0.29</version>         </dependency>         <dependency>             <groupId>org.apache.poi</groupId>             <artifactId>poi-ooxml</artifactId>             <version>5.2.3</version>         </dependency>    </dependencies>     ```2. **方案二(LibreOffice)** 
- **优点**:保留原始格式,转换质量高   
- **缺点**:需安装 LibreOffice   
- **命令调整**:    - Windows:确保 `soffice.exe` 路径正确     - Linux/macOS:终端直接执行 `libreoffice --version` 测试是否安装成功
# 执行步骤
1. 将代码中的路径替换为实际 PDF 文件夹和输出文件夹
2. 根据选择的方案添加依赖或安装 LibreOffice
3. 运行程序,查看目标文件夹生成的 Word 文件> **注意**:批量转换大量文件时,建议增加错误处理机制(如文件名合法性检查、转换失败重试等)。对于复杂格式的 PDF,商业库(如 Aspose.PDF)效果更好,但需要付费。
http://www.dtcms.com/wzjs/188119.html

相关文章:

  • 建立大型网站吗成人用品网店进货渠道
  • 大连市政府采购网招标公告seo案例模板
  • 学生网站建设的基本流程优化大师手机版
  • 中国做室内设计的网站关键词搜索量查询
  • 太原市做网站wordpress免费网站
  • 专门网站建设可以发外链的网站整理
  • 哪些网站可以做行程智慧软文发稿平台
  • 做公众号封面的网站黄冈网站推广软件有哪些
  • 做熊猫tv网站的目的营销方案怎么写?
  • 仿牌网站流量网站制作厂家有哪些
  • 杭州百度网站建设高级seo
  • 网页图片不显示seo主要优化哪些
  • 做网站的主营业务国际网络销售平台有哪些
  • 网站推广策划方案模板谷歌paypal官网登录入口
  • 简单的网页设计代码记事本网站的优化从哪里进行
  • 怎么做幼儿园网站介绍ppt宁波seo高级方法
  • 怎么样建立自己的视频网站网络营销实施方案
  • 医药行业网站建设如何在百度提交网站
  • 2010年青海省建设厅网站今日头条新闻10条简短
  • 周口做网站哪家好石家庄邮电职业技术学院
  • 西藏建设注册中心官方网站seo短视频保密路线
  • 深圳市宝安网站建设h5制作
  • 网站添加关键字seo推广思路
  • 微信网站图片链接怎么做网络服务器的功能
  • 常平哪里有招计算机网站开发的外贸网站推广平台
  • wordpress4 sqlitewin7系统优化工具
  • 网站开发功能确认单b站推广网站入口
  • 厦门酒店网站建设新闻稿件
  • 网站开发 模版绑定顶级域名搜索引擎有哪些软件
  • 网站制作中心网站建设公司苏州