1、下载xpdf最新版本,地址:http://www.foolabs.com/xpdf/download.html
2、下载中文支持包
我下载的是xpdf-chinese-simplified.tar.gz
3、下载pdftohtml支持包
地址:http://sourceforge.net/projects/pdftohtml/
我下载的是:pdftohtml-0.39-win32.tar.gz
4、解压调试
1) 先将xpdf-3.02pl2-win32.zip解压,解压后的内容可根据需要进行删减,如果只需要转换为txt格式,其他的exe文件可以删除,只保留pdftotext.exe,以此类推;
2) 然后将xpdf-chinese-simplified.tar.gz解压到刚才xpdf-3.02pl2-win32.zip的解压目录;
3) 将pdftohtml-0.39-win32.tar.gz解压,pdftohtml.exe解压到xpdf-3.02pl2-win32.zip的解压目录;
4) 目录结构:
+---[X:\xpdf]
|-------各种转换用到的exe文件
|
|-------xpdfrc
|
+------[X:\xpdf\xpdf-chinese-simplified]
|
|
+-------很多转换时需要用到的字符文件
xpdfrc:此文件是用来声明转换字符集对应路径的文件
5) 修改xpdfrc文件(文件原名为sample-xpdfrc)
修改文件内容为:
#----- begin Chinese Simplified support package
cidToUnicode Adobe-GB1 xpdf-chinese-simplified\Adobe-GB1.cidToUnicode
unicodeMap ISO-2022-CN xpdf-chinese-simplified\ISO-2022-CN.unicodeMap
unicodeMap EUC-CN xpdf-chinese-simplified\EUC-CN.unicodeMap
unicodeMap GBK xpdf-chinese-simplified\GBK.unicodeMap
cMapDir Adobe-GB1 xpdf-chinese-simplified\CMap
toUnicodeDir xpdf-chinese-simplified\CMap
fontDir C:\WINDOWS\Fonts
displayCIDFontTT Adobe-GB1 C:\WINDOWS\Fonts\simhei.ttf
#----- end Chinese Simplified support package
6) 创建bat文件pdftohtml.bat(放置的路径不能包含空格)内容为:
@echo off
set folderPath=%1
set filePath=%2
cd /d %folderPath%
pdftohtml -enc GBK %filePath%
exit
7) 创建类
package com.xpdf; import java.io.File; import java.io.IOException; public class PdfToHtml_xpdf { private static String INPUT_PATH; private static String PROJECT_PATH; public static void convertToHtml(String file, String project) { INPUT_PATH = file; PROJECT_PATH = project; if (checkContentType() == 0) { toHtml(); } } private static int checkContentType() { String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1, INPUT_PATH.length()).toLowerCase(); System.out.println("type:" + type); if (type.equals("pdf")){ return 0;} else { return 9; } } private static void toHtml() { if (new File(INPUT_PATH).isFile()) { try { String cmd ="cmd /k start D:\\testOffice\\xpdf\\pdftohtml.bat \"" + INPUT_PATH + "\" \"" + PROJECT_PATH + "\""; System.out.println("cmd:" + cmd); Runtime.getRuntime().exec(cmd); System.out.println("OK"); } catch (IOException e) { e.printStackTrace(); System.out.println("error"); } } } }
String cmd = "....";此处代码是调用创建的bat文件进行转换
8) 测试转换
package com.xpdf; public class RunXpdf { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PdfToHtml_xpdf.convertToHtml("D:\\testOffice\\xpdf\\超经典的设计模式讲解.pdf", "D:\\testOffice\\xpdf\\xpdf3"); } }