想制作一个能读取PDF文件的程序
要把PDF文件内容显示在JTextArea上。
安装了xpdf-3.02-win32.zip
xpdf-chinese-simplified.tar.gz苦于没有例子程序,一直没有调试成功。这是找到的一个例子程序,但没有输出,望有人帮忙改下,谢谢先!
[code] public class PdfWin {
public PdfWin() {
}
public static void main(String args[]) throws Exception
{
String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";
String filename="c:\\a.pdf";
String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
StringWriter out = new StringWriter();
char [] buf = new char[10000];
int len;
while((len = reader.read(buf))>= 0) {
//out.write(buf, 0, len);
System.out.println("the length is"+len);
}
reader.close();
String ts=new String(buf);
System.out.println("the str is"+ts);
}
} [/code]
要把PDF文件内容显示在JTextArea上。
安装了xpdf-3.02-win32.zip
xpdf-chinese-simplified.tar.gz苦于没有例子程序,一直没有调试成功。这是找到的一个例子程序,但没有输出,望有人帮忙改下,谢谢先!
[code] public class PdfWin {
public PdfWin() {
}
public static void main(String args[]) throws Exception
{
String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";
String filename="c:\\a.pdf";
String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
StringWriter out = new StringWriter();
char [] buf = new char[10000];
int len;
while((len = reader.read(buf))>= 0) {
//out.write(buf, 0, len);
System.out.println("the length is"+len);
}
reader.close();
String ts=new String(buf);
System.out.println("the str is"+ts);
}
} [/code]
解决方案 »
- 关于解析时间字符串
- java的网络编程,创建SeverSocket如何使用?用什么类型的输入输出流
- 在jsp中加载applet,显示找不到class的错误?
- 请教Java集合的问题
- 帮忙看看socket客户端问题,谢谢!
- 在线等 Java 中格式化I/O 是什么?
- Hello World运行不了!!!!!!!!!!!!!!!!!!!!!
- 怎样判断输入的字符串是否是“YYYY-MM-DD”格式的?
- JBuilder7中无法连接oracle数据库
- 下面这个小程序为什么执行COPY这个字符串不对,而执行net send确正确呢
- 请教高手关于C++调用java的方法,调试时 JNI_CreateJavaVM出错
- response 奇怪需求
import org.pdfbox.pdfparser.PDFParser;
import java.io.*;
import org.pdfbox.util.PDFTextStripper;
import java.util.Date;
/**
* <p>Title: pdf extraction</p>
* <p>Description: email:[email protected]</p>
* <p>Copyright: Matrix Copyright (c) 2003</p>
* <p>Company: Matrix.org.cn</p>
* @author chris
* @version 1.0,who use this example pls remain the declare
*/ public class PdfExtracter{ public PdfExtracter(){
}
public String GetTextFromPdf(String filename) throws Exception
{
String temp=null;
PDdocument.nbsppdfdocument.null;
FileInputStream is=new FileInputStream(filename);
PDFParser parser = new PDFParser( is );
parser.parse();
pdfdocument.nbsp= parser.getPDdocument.);
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter( out );
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(pdfdocument.getdocument.), writer );
writer.close();
byte[] contents = out.toByteArray(); String ts=new String(contents);
System.out.println("the string length is"+contents.length+"\n");
return ts;
}
public static void main(String args[])
{
PdfExtracter pf=new PdfExtracter();
PDdocument.nbsppdfdocument.nbsp= null; try{
String ts=pf.GetTextFromPdf("c:\\a.pdf");
System.out.println(ts);
}
catch(Exception e)
{
e.printStackTrace();
}
} }
所以才想用xpdf。