如何使用xpdf把本地磁盘上的pdf转化为txt

来源:互联网 发布:在韩国能用淘宝吗 编辑:程序博客网 时间:2024/05/18 02:21

一:下载工具包:


可到该账户下下载

二:工作路径设置:


三:文件配置:

打开此文件,修改文件配置:

1:修改1:

在最后加上:
#----- begin Chinese Simplified support package (2004-jul-27)  
  
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  /usr/..../gkai00mp.ttf  
  
#----- end Chinese Simplified support package  

2:修改2:

在 text output control下添加:
#----- text output control
# If set to "yes", text extraction will  insert  page  
# breaks  (form feed characters) between pages.  This  
# defaults to "yes".  
textPageBreaks      no  

3:修改3:

取消对编码的注释:
textEncoding UTF-8

四:在Myeclipse中启动工程

1:创建工程:

2:源码展示:
XpdfToText.java

package pdfToText;
import java.io.File;  
import java.io.IOException;  
public class XpdfToText {
// PDF文件名  
    private File pdffile;  
    // 转换器的存放位置,默认在c:\xpdftest\xpdf下面  
    private String CONVERTOR_STORED_PATH = "c:\\xpdftest\\xpdf\\";  
    // 转换器的名称,默认为pdftotext  
    private String CONVERTOR_NAME = "pdftotext";  
  
    // 构造函数,参数为pdf文件的路径  
    public XpdfToText(String pdffile) throws IOException {  
        this.pdffile = new File(pdffile);  
    }  
  
    // 将pdf转为文本文档,参数为目标文件的路径  
    public void toTextFile(String targetfile) throws IOException {  
        toTextFile(targetfile, true);  
    }  
  
    // 将pdf转为文本文档,参数1为目标文件的路径,  
    // 参数2为true则表示使用PDF文件中的布局  
    public void toTextFile(String targetfile, boolean isLayout)  
            throws IOException {  
        String[] cmd = getCmd(new File(targetfile), isLayout);  
        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 };  
    }  

}  

 TestXpdfToText.java
package pdfToText;

public class TestPdfToText {
public static void main(String[] args) {  
       String rootPath = "c:\\Nutch入门教程";  
       //pdf文件路径  
       String pdffile = rootPath + ".pdf";  
       //用xpdf生成的txt文件路径  
       String xpdfToTxtfile = rootPath + "_xpdf.txt";  
       //用pdfbox生成的txt文件路径  
       String pdfboxToTxtfile = rootPath + "_pdfbox.txt";  
       //XPDF  
       try{  
           long begin = System.currentTimeMillis();   
           XpdfToText xpToTxt = new XpdfToText(pdffile);  
           xpToTxt.toTextFile(xpdfToTxtfile);  
           long end = System.currentTimeMillis();   
           System.out.println("xpdf\t cost:\t" + (end - begin) + " ms");  
       }catch(Exception e){  
           e.printStackTrace();  
       }  
   }  
}

3:运行:
运行后即可在C盘目录下看到转换后的txt



下面与luncen结合,下回讲


0 0