航空论坛_航空翻译_民航英语翻译_飞行翻译

 找回密码
 注册
搜索
查看: 1470|回复: 0
打印 上一主题 下一主题

使用xpdf来处理中文PDF文档 [复制链接]

Rank: 9Rank: 9Rank: 9

跳转到指定楼层
1#
发表于 2011-11-15 13:34:16 |只看该作者 |倒序浏览
下载:xpdf xpdf-chinese-simplified2 I4 x8 C; J& G0 V! e$ ]
         xpdf-chinese-traditional.tar.gz
# F1 u: `/ Y& S: K
下载两个中文字体文件 字体文件4 }5 w; q) V- w* W1 D+ N

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

图7-8 Xpdf解压后的目录

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

代码7.3

3 |! P  U, d1 i! k% }0 A3 {8 D1 X

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来进行文本的提取。其具体实现代码如下。

代码7.4

" R7 k2 c( Z0 C0 g% P4 M9 }

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文件的内容

7.2.4 运行效果

下面通过一个函数来测试Pdf2Text类,在ch7.xpdf包中新建一个Pdf2TextTest类,包含一个main函数,其代码如下。

代码7.5

0 }+ |! g" E* d

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 用于转换的中文PDF文件

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

source:http://book.csdn.net/bookfiles/312/10031212844.shtml

- V( U+ i" ~# z

相关帖子

您需要登录后才可以回帖 登录 | 注册


Archiver|航空论坛 ( 渝ICP备10008336号 )

GMT+8, 2024-6-16 01:25 , Processed in 0.031200 second(s), 11 queries .

Powered by Discuz! X2

© 2001-2011 MinHang.CC.

回顶部