java pdf转成word文档_如何使用Java将pdf文件转换为word文件

本文介绍了如何使用Java将PDF文件转换为Word文档。通过PDFParser解析PDF,获取文本内容,然后将其写入到新的Word文件中。
摘要由CSDN通过智能技术生成

小编典典

public class PDFTextReader

{

static String pdftoText(String fileName) {

PDFParser parser;

String parsedText = null;

PDFTextStripper pdfStripper = null;

PDDocument pdDoc = null;

COSDocument cosDoc = null;

File file = new File(fileName);

if (!file.isFile()) {

System.err.println("File " + fileName + " does not exist.");

return null;

}

try {

parser = new PDFParser(new FileInputStream(file));

} catch (IOException e) {

System.err.println("Unable to open PDF Parser. " + e.getMessage());

return null;

}

try {

parser.parse();

cosDoc = parser.getDocument();

pdfStripper = new PDFTextStripper();

pdDoc = new PDDocument(cosDoc);

parsedText = pdfStripper.getText(pdDoc);

} catch (Exception e) {

System.err

.println("An exception occured in parsing the PDF Document."

+ e.getMessage());

} finally {

try {

if (cosDoc != null)

cosDoc.close();

if (pdDoc != null)

pdDoc.close();

} catch (Exception e) {

e.printStackTrace();

}

}

return parsedText;

}

public static void main(String args[]){

try {

String content = pdftoText(PDF_FILE_PATH);

File file = new File("/sample/filename.txt");

// if file doesnt exists, then create it

if (!file.exists()) {

file.createNewFile();

}

FileWriter fw = new FileWriter(file.getAbsoluteFile());

BufferedWriter bw = new BufferedWriter(fw);

bw.write(content);

bw.close();

System.out.println("Done");

} catch (IOException e) {

e.printStackTrace();

}

}

}

2020-11-13

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值