1:添加依赖
4.0.0
pdfToWord
pdfToWord
1.0-SNAPSHOT
commons-logging
commons-logging
1.2
org.apache.pdfbox
fontbox
2.0.11
com.levigo.jbig2
levigo-jbig2-imageio
2.0
org.apache.pdfbox
pdfbox-tools
2.0.11
commons-io
commons-io
2.6
2:编写转换的方法
importorg.apache.pdfbox.pdmodel.PDDocument;importorg.apache.pdfbox.text.PDFTextStripper;import java.io.*;/*** 把pdf转换为word格式
*
*@authorAngin
* @date 2019/3/18 0018.*/
public classPdfToWord {/*** 转换*/
public voidconvertText(String pdfPath) {
PDDocument doc= null;
OutputStream fos= null;
Writer writer= null;
PDFTextStripper stripper= null;try{
doc= PDDocument.load(newFile(pdfPath));
fos= new FileOutputStream(pdfPath.substring(0, pdfPath.indexOf(".")) + ".doc");
writer= new OutputStreamWriter(fos, "UTF-8");
stripper= newPDFTextStripper();int pageNumber =doc.getNumberOfPages();
stripper.setSortByPosition(true);
stripper.setStartPage(1);
stripper.setEndPage(pageNumber);
stripper.writeText(doc, writer);
writer.close();
doc.close();
}catch(IOException e) {
e.printStackTrace();
}
System.out.println("end..");
}
}
3:main方法中进行测试
/*** main方法测试
*@authorAngin
* @date 2019/3/18 0018.*/
public classMainClass {public static voidmain(String[] args) {
PdfToWord convert=newPdfToWord();
convert.convertText("E:\\pdfToWord.pdf");
}
}
此方法只适合文档型的pdf转换,如果图片的话,转换后无法读取。