XPDF的配置
1.工具包下载
点这里 下载
我下载的是: xpdf-3.02pl4-win32.zip
另外还需要一个语言包: xpdf-chinese-simplified.tar.gz
2.工作路径设置
参考资料点这里
在本文中以 c:\xpdftest\xpdf 作为xpdf的工作路径。
将 xpdf-3.02p14-win32.zip 解压到 c:\xpdftest\xpdf 下。
将 xpdf-chinese-simplified.tar.gz 解压到 c:\xpdftest\xpdf\xpdf-chinese-simplified 下。
修改配置文件
为了启用中文简体语言包,必须将 xpdf-chinese-simplified 目录下的 sample-xpdfrc 文件进行如下配置,并将其另存为 xpdfrc 文件 。
注意:此文件为配置文件,而且名称必须是 xpdfrc 。如果是别的名字,即使调用 pdftotext.exe 时,传入 ” -cfg xpdfrc2 ” 来告诉 xpdf 配置文件的名字,好像 pdftotext.exe 也并没有使用这个配置文件。所以为了减少误解,请您将配置文件直接命名为 xpdfrc 。
修改 sample-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:/xpdf/chinese-simplified/CMap - #displayCIDFontTT
Adobe-GB1 C:\WINDOWS\Font\simhei.ttf
注意:路径要跟自己配置的一样。
修改 sample-xpdfrc 文件之二
另外,配置文件中原先没有加上一个“ textPageBreaks ”控制。为了避免这个分页符号,我们需要在 sample-xpdfrc 文件 “ text output control ”下面 加上一段话 :
- #
If set to "yes", text extraction will insert page - #
breaks (form feed characters) between pages. This - #
defaults to "yes". - textPageBreaks
no
设置 textPageBreaks 为 no 的意思是:在 PDF 文档的两页之间不加入分页符号。 之所以这样,是因为这个符号有时候会引起 SAX 解析 XML 上的困难。
修改 sample-xpdfrc 文件之三
配置文件中原先把 textEncoding 注释了。这样默认的字符集是 Latin1 。我们必须打开它,并且就是指定 textEncoding 为 UTF-8 ,而不是 GB2312 。即修改这句话 :
textEncoding
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 };
}
}
表示是否采用原始的PDF文件中的layout布局。类中的getCmd()方法负责解析传进来的参
数,并生成一个String数组。该String数组表示一个操作系统中的命令,其中,各项分别代
表命令后所跟的参数。在获得到该数组后,再调用Runtime.getRuntime().exec(String[])函数
来执行命令。
注意:在getCmd()方法中设置编码方式的时候,用到的是GBK。这并不是对所有的文件都
适用,因为中文的编码方式不只一种,读者可以根据PDF的编码类型选择不同的encoding
方式。所有简体中文的编码方式都定义在文件xpdfrc中的unicodeMap中,现在支持3种编
码方式,分别是ISO-2022-CN,EUC-CN,GBK。
下面通过一个函数来测试Pdf2Text类,在ch7.xpdf包中新建一个Pdf2TextTest类,包含一个
main函数,其代码如下。
代码 7.5
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();
}
}
}