`
sealbird
  • 浏览: 572380 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

强劲开源的XPDF 处理pdf2txt

 
阅读更多
2008-09-17 22:47
转自:http://book.csdn.net/bookfiles/312/10031212844.shtml

使用xpdf来处理中文PDF文档 

一、PDFBox看起来非常的方便,它的API功能强大。甚至能和Lucene进行无缝的结合。但是它有一个致命的弱点,就是它不支持中文。要提取中文的文本,可以采用另一个非常出色的工具xpdf。

二、xpdf的下载

读者可以到http://www.foolabs.com/xpdf/download.html下载最新版本的xpdf。如图7-7所示



本书采用的是xpdf-3.01pl2-win32.zip。另外,还需要下载一个中文包xpdf-chinese-simplified.tar.gz。

三、配置

将xpdf-3.01pl2-win32.zip解压到c:\xpdftest目录下,然后将xpdf-chinese-simplified.tar.gz解压倒c:\xpdftest\xpdf\目录下,解压后的目录结构如图7-8所示。




打开目录下的xpdfrc文件,编辑文件内容,如下代码所示。

代码7.3

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。

四、提取中文

在工程中新建一个ch7.xpdf包,并创建一个Pdf2Text类。该类采用Runtime传入参数,调用pdftotext.exe来进行文本的提取。其具体实现代码如下。

代码7.4

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所示。



          图7-9 xpdfrc文件的内容


五、运行效果

下面通过一个函数来测试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();

        }

    }

}

用于转换的PDF文件如图7-10所示。



                  图7-10

代码7.5运行之后,结果如图7-11所示。



                                  图7-11 运行结果
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics