用xpdf转换pdf为text

使用xpdf来处理中文PDF文档
  package ch7.xpdf;

import java.io.*;

public class Pdf2Text {
// PDF文件名
private File pdffile;
// 转换器的存放位置,默认在c:\xpdf下面
private String CONVERTOR_STORED_PATH = "c:\\xpdf";
// 转换器的名称,默认为pdftotext
private String CONVERTOR_NAME = "pdftotext";
// 构造函数,参数为pdf文件的路径
public Pdf2Text(String pdffile) throws IOException {
this(new File(pdffile));
}
// 构造函数,参数为pdf文件的对像
public Pdf2Text(File pdffile) throws IOException {
this.pdffile = pdffile;
}
// 将pdf转为文本文档
public void toTextFile() throws IOException {
toTextFile(pdffile, true);
}
// 将pdf转为文本文档,参数为目标文件的路径,默认使用PDF文件中的布局
public void toTextFile(String targetfile) throws IOException {
toTextFile(new File(targetfile), true);
}
// 将pdf转为文本文档,参数1为目标文件的路径,
// 参数2为true则表示使用PDF文件中的布局
public void toTextFile(String targetfile, boolean isLayout)
throws IOException {
toTextFile(new File(targetfile), isLayout);
}

// 将pdf转为文本文档,参数为目标文件
public void toTextFile(File targetfile) throws IOException {
toTextFile(targetfile, true);
}
// 将pdf转为文本文档,参数1为目标文件,
// 参数2为true则表示使用PDF文件中的布局
public void toTextFile(File targetfile, boolean isLayout)
throws IOException {
String[] cmd = getCmd(targetfile, isLayout);
Process p = Runtime.getRuntime().exec(cmd);
}
// 获取PDF转换器的路径
public String getCONVERTOR_STORED_PATH() {
return CONVERTOR_STORED_PATH;
}
// 设置PDF转换器的路径
public void setCONVERTOR_STORED_PATH(String path) {
if (!path.trim().endsWith("\\"))
path = path.trim() + "\\";
this.CONVERTOR_STORED_PATH = path;
}
// 解析命令行参数
private String[] getCmd(File targetfile, boolean isLayout) {
// 命令字符
String command = CONVERTOR_STORED_PATH + CONVERTOR_NAME;
// PDF文件的绝对路径
String source_absolutePath = pdffile.getAbsolutePath();
// 输出文本文件的绝对路径
String target_absolutePath = targetfile.getAbsolutePath();
// 保持原来的layout
String layout = "-layout";
// 设置编码方式
String encoding = "-enc";
String character = "GBK";
// 设置不打印任何消息和错误
String mistake = "-q";
// 页面之间不加入分页
String nopagebrk = "-nopgbrk";
// 如果isLayout为false,则设置不保持原来的layout
if (!isLayout)
layout = "";
return new String[] { command, layout, encoding, character, mistake,
nopagebrk, source_absolutePath, target_absolutePath };
}
}


package ch7.xpdf;

import java.io.*;

public class Pdf2TextTest {
public static void main(String[] args) {
try {
// 参数输入PDF文件的存放位置
Pdf2Text p2t = new Pdf2Text("c:\\test.pdf");
// 设定转换器的位置
p2t.setCONVERTOR_STORED_PATH("c:\\xpdftest\\xpdf");
// 设置文本文件存放位置
p2t.toTextFile("c:\\test.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值