图7-8 Xpdf解压后的目录
打开目录下的xpdfrc文件,编辑文件内容,如下代码所示。
cidToUnicode Adobe-GB1 c:\xpdftest\xpdf\xpdf-chinese-simplified\Adobe-GB1.cidToUnicode
unicodeMap ISO-2022-CN c:\xpdftest\xpdf\xpdf-chinese-simplified\ISO-2022-CN.unicodeMap
unicodeMap EUC-CN c:\xpdftest\xpdf\xpdf-chinese-simplified\EUC-CN.unicodeMap
unicodeMap GBK c:\xpdftest\xpdf\xpdf-chinese-simplified\GBK.unicodeMap
cMapDir Adobe-GB1 c:\xpdftest\xpdf\xpdf-chinese-simplified\CMap
toUnicodeDir c:\xpdftest\xpdf\xpdf-chinese-simplified\CMap
fontDir C:\WINDOWS\Fonts
displayCIDFontTT Adobe-GB1 C:\WINDOWS\Fonts\simhei.ttf
textEOL CR+LF
文件的路径读者可以根据自己的环境改,如在windows 2000下,fontDir所在的位置是C:\WINNT\Fonts,displayCIDFontTT Adobe-GB1的位置是在C:\WINNT\Fonts\simhei.ttf。
7.2.3 提取中文
在工程中新建一个ch7.xpdf包,并创建一个Pdf2Text类。该类采用Runtime传入参数,调用pdftotext.exe来进行文本的提取。其具体实现代码如下。
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 };
}
}
该类对外提供一个toTextFile()方 法。该方法接收2个参数:targetfile为目标PDF文件,isLayout表示是否采用原始的PDF文件中的layout布局。类中的 getCmd()方法负责解析传进来的参数,并生成一个String数组。该String数组表示一个操作系统中的命令,其中,各项分别代表命令后所跟的 参数。在获得到该数组后,再调用Runtime.getRuntime().exec(String[])函数来执行命令。
注意:在getCmd()方法中设置编码方式的 时候,用到的是GBK。这并不是对所有的文件都适用,因为中文的编码方式不只一种,读者可以根据PDF的编码类型选择不同的encoding方式。所有简 体中文的编码方式都定义在文件xpdfrc中的unicodeMap中,现在支持3种编码方式,分别是ISO-2022-CN,EUC-CN,GBK,如 图7-9所示。