导入依赖:
Apache PDFBox提供了处理PDF文档的功能。
Apache POI支持处理Word、Excel和.ppt。
<dependencies><!-- Apache PDFBox --><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.27</version></dependency><!-- Apache POI --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.0.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.0.0</version></dependency></dependencies>
代码实现:
import org.apache.poi.xwpf.usermodel.*;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.text.PDFTextStripper;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class PdfToWordConverter {public static void convertPdfToWord(String pdfFilePath, String docxFilePath) {try {// 加载PDF文档PDDocument document = PDDocument.load(new FileInputStream(pdfFilePath));// 创建Word文档XWPFDocument doc = new XWPFDocument();// 提取PDF文本内容PDFTextStripper stripper = new PDFTextStripper();String text = stripper.getText(document);// 创建段落并添加文本内容XWPFParagraph paragraph = doc.createParagraph();XWPFRun run = paragraph.createRun();run.setText(text);// 保存Word文档FileOutputStream out = new FileOutputStream(docxFilePath);doc.write(out);out.close();// 关闭文档document.close();doc.close();System.out.println("PDF转Word成功!");} catch (IOException e) {System.out.println("PDF转Word失败:" + e.getMessage());}}// 在main方法中调用convertPdfToWord方法,传入PDF文件的路径和要生成的Word文档的路径public static void main(String[] args) {String pdfFilePath = "input.pdf";String docxFilePath = "output.docx";convertPdfToWord(pdfFilePath, docxFilePath);}}