java读取pdf文件内容
在java中要读取pdf文件内容,我们可以借助第三方软件实现。常用的是xpdf,本文就简单介绍在linux下如何安装xpdf,及在java中如何利用xpdf读取pdf文件内容。一.安装xpdf
在fc系列下,不用安装,可以直接yum,但是笔者建议还是下载安装的好,因为笔者曾经碰到过这样的问题,客户服务器上的xpdf是yum安装的,有一些特殊的pdf文件就无法预览,但是将yum安装的xpdf卸载,然后下载xpdf安装程序,再重新安装后,就可以了。
1.下载
ok,我们需要下载的xpdf安装包主要有三个:
(1)进入下载目录,将主程序解压至/usr,也可以是其他地方,根据个人情况而定。
然后将其重命名,这样看起来简单点
(2)建立中文支持。回到下载目录,依次执行:
(3)配置环境
增加如下内容
export PATH=/usr/xpdf/:$PATH
确保重启机器后,在控制台输入xpdf不会提示找不到命令或文件即可。
(4)资源配置
*在文件开始处增加如下内容(将/usr/xpdf替换为xpdf的实际路径)*
然后再执行:
好了,到这里我们也就安装完成了。下面介绍如何利用xpdf读取pdf文件的内容
二.利用xpdf读取pdf文件的内容
1.下载
ok,我们需要下载的xpdf安装包主要有三个:
主程序:
ftp://ftp.foolabs.com/pub/xpdf/xpdf-3.01pl2-linux.tar.gz
简体中文支持: ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz
繁体中文支持: ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-traditional.tar.gz
2.安装部署
简体中文支持: ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz
繁体中文支持: ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-traditional.tar.gz
(1)进入下载目录,将主程序解压至/usr,也可以是其他地方,根据个人情况而定。
#
tarzvfxxpdf-
3
.
01pl2-linux
.
tar
.
gz-C
/
usr
# cd usr
# cd usr
mvxpdf-
3
.
01pl2-linux
/
xpdf
#
tarzvfxxpdf-chinese-simplified
.
tar
.
gz-C
/
usr
/
xpdf
# mv / usr / xpdf / xpdf-chinese-simplified / usr / xpdf / chinese-simplified
# tarzvfxxpdf-chinese-traditional . tar . gz-C / usr / xpdf
# mv / usr / xpdf / xpdf-chinese-traditional / usr / xpdf / chinese-traditional
# mv / usr / xpdf / xpdf-chinese-simplified / usr / xpdf / chinese-simplified
# tarzvfxxpdf-chinese-traditional . tar . gz-C / usr / xpdf
# mv / usr / xpdf / xpdf-chinese-traditional / usr / xpdf / chinese-traditional
#
vi
/
etc
/
bashrc
export PATH=/usr/xpdf/:$PATH
确保重启机器后,在控制台输入xpdf不会提示找不到命令或文件即可。
(4)资源配置
#
cd
/
usr
/
xpdf
# cpsample-xpdfrcxpdfrc
# vixpdfrc
# cpsample-xpdfrcxpdfrc
# vixpdfrc
#
-----beginChineseSimplifiedsupportpackage
(
2004
-jul-
27
)
cidToUnicodeAdobe-GB1 " /usr/xpdf/chinese-simplified/Adobe-GB1.cidToUnicode "
unicodeMapISO- 2022 -CN " /usr/xpdf/chinese-simplified/ISO-2022-CN.unicodeMap "
unicodeMapEUC-CN " /usr/xpdf/chinese-simplified/EUC-CN.unicodeMap "
unicodeMapGBK " /usr/xpdf/chinese-simplified/GBK.unicodeMap "
cMapDirAdobe-GB1 " /usr/xpdf/chinese-simplified/CMap "
toUnicodeDir " /usr/xpdf/chinese-simplified/CMap "
# displayCIDFontTTAdobe-GB1 / usr /..../ gkai00mp . ttf
# ----- end ChineseSimplifiedsupportpackage
# -----beginChineseTraditionalsupportpackage ( 2004 -jul- 27 )
cidToUnicodeAdobe-CNS1 " /usr/xpdf/chinese-traditional/Adobe-CNS1.cidToUnicode "
unicodeMapBig5 " /usr/xpdf/chinese-traditional/Big5.unicodeMap "
unicodeMapBig5ascii " /usr/xpdf/chinese-traditional/Big5ascii.unicodeMap "
cMapDirAdobe-CNS1 " /usr/xpdf/chinese-traditional/CMap "
toUnicodeDir " /usr/xpdf/chinese-traditional/CMap "
# displayCIDFontTTAdobe-CNS1 / usr /..../ bkai00mp . ttf
# ----- end ChineseTraditionalsupportpackage
cidToUnicodeAdobe-GB1 " /usr/xpdf/chinese-simplified/Adobe-GB1.cidToUnicode "
unicodeMapISO- 2022 -CN " /usr/xpdf/chinese-simplified/ISO-2022-CN.unicodeMap "
unicodeMapEUC-CN " /usr/xpdf/chinese-simplified/EUC-CN.unicodeMap "
unicodeMapGBK " /usr/xpdf/chinese-simplified/GBK.unicodeMap "
cMapDirAdobe-GB1 " /usr/xpdf/chinese-simplified/CMap "
toUnicodeDir " /usr/xpdf/chinese-simplified/CMap "
# displayCIDFontTTAdobe-GB1 / usr /..../ gkai00mp . ttf
# ----- end ChineseSimplifiedsupportpackage
# -----beginChineseTraditionalsupportpackage ( 2004 -jul- 27 )
cidToUnicodeAdobe-CNS1 " /usr/xpdf/chinese-traditional/Adobe-CNS1.cidToUnicode "
unicodeMapBig5 " /usr/xpdf/chinese-traditional/Big5.unicodeMap "
unicodeMapBig5ascii " /usr/xpdf/chinese-traditional/Big5ascii.unicodeMap "
cMapDirAdobe-CNS1 " /usr/xpdf/chinese-traditional/CMap "
toUnicodeDir " /usr/xpdf/chinese-traditional/CMap "
# displayCIDFontTTAdobe-CNS1 / usr /..../ bkai00mp . ttf
# ----- end ChineseTraditionalsupportpackage
#
cpxpdfrc
/
usr
/
local
/
etc
/
方法很简单,利用著名的Runtime.getRuntime()即可,如下:
/**
* @param filePathpdf文件路径
* @return
*/
public StringgetPdfContent(StringfilePath) {
Stringexcute = " pdftotext " ;
String[]cmd = new String[] {excute, " -enc " , " UTF-8 " , " -q " ,filePath, " - " } ;
Processp = null ;
try {
p = Runtime.getRuntime().exec(cmd);
} catch (IOExceptione) {
e.printStackTrace();
}
BufferedInputStreambis = new BufferedInputStream(p.getInputStream());
InputStreamReaderreader = null ;
try {
reader = new InputStreamReader(bis, " UTF-8 " );
} catch (UnsupportedEncodingExceptione1) {
e1.printStackTrace();
}
StringBuffersb = new StringBuffer();
try {
BufferedReaderbr = new BufferedReader(reader);
Stringline = br.readLine();
sb = new StringBuffer();
while (line != null ) {
sb.append(line);
sb.append( " " );
line = br.readLine();
}
} catch (Exceptione) {
e.printStackTrace();
}
return sb.toString();
}
* @param filePathpdf文件路径
* @return
*/
public StringgetPdfContent(StringfilePath) {
Stringexcute = " pdftotext " ;
String[]cmd = new String[] {excute, " -enc " , " UTF-8 " , " -q " ,filePath, " - " } ;
Processp = null ;
try {
p = Runtime.getRuntime().exec(cmd);
} catch (IOExceptione) {
e.printStackTrace();
}
BufferedInputStreambis = new BufferedInputStream(p.getInputStream());
InputStreamReaderreader = null ;
try {
reader = new InputStreamReader(bis, " UTF-8 " );
} catch (UnsupportedEncodingExceptione1) {
e1.printStackTrace();
}
StringBuffersb = new StringBuffer();
try {
BufferedReaderbr = new BufferedReader(reader);
Stringline = br.readLine();
sb = new StringBuffer();
while (line != null ) {
sb.append(line);
sb.append( " " );
line = br.readLine();
}
} catch (Exceptione) {
e.printStackTrace();
}
return sb.toString();
}