想制作一个能读取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]

解决方案 »

  1.   

    import org.pdfbox.pdmodel.PDdocument. 
    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(); 

    } } 
      

  2.   

    谢谢zuguanqun的回复,这个代码我也看了,可惜pdfbox不支持中文PDF,
    所以才想用xpdf。
      

  3.   

    读取pdf文件的开发包java里面有好几个,你随意选择一个就可以了。
      

  4.   

    系统中装上openoffice,然后用Runtime.getRuntime().exec()就可以得到了