使用java api 简单实现pdf文档转word文档功能。
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;import java.io.*;public class PdfToWord {public static void main(String[] args) {String pdfPath = "D:\\Tools\\Spring.pdf";try {PDDocument doc = PDDocument.load(new File(pdfPath));int pageNum = doc.getNumberOfPages();pdfPath = pdfPath.substring(0, pdfPath.lastIndexOf("."));String fileName = pdfPath+".doc";File file = new File(fileName);if(!file.exists()){file.createNewFile();}FileOutputStream fos = new FileOutputStream(file);Writer writer = new OutputStreamWriter(fos,"UTF-8");PDFTextStripper textStripper = new PDFTextStripper();textStripper.setSortByPosition(true);textStripper.setStartPage(1);textStripper.setEndPage(pageNum);textStripper.writeText(doc,writer);writer.close();doc.close();System.out.println("转换成功");} catch (IOException e) {e.printStackTrace();}}}
本文所用的jar包,pdfbox-1.8.2.jar,jdk 1.8 ,由于使用gradle构建 ,compile("org.apache.pdfbox:pdfbox:1.8.2"),也可以自行下载jar包。
本来有个pdf文档90多页,但是有没有标签,阅读麻烦,作为一个程序员,果断使用程序实现,结果转换过来的文件没有格式,没有达到预期效果。