pdf转文字java_java实现pdf转word(文字)

本文介绍了使用Apache PDFBox库将PDF文档转换为Word(文本)的Java实现方法。通过加载PDF文件,提取文本,然后将其写入新的DOC文件中完成转换。注意,这种方法仅适用于包含可读文本的PDF,对于包含图片的PDF可能无法有效转换。
摘要由CSDN通过智能技术生成

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转换,如果图片的话,转换后无法读取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值