使用PDFBOX提取PDF文件

最近在使用lucene建立索引时,要使用PDFBOX提取PDF文件,但结果总是报错:

java.lang.Throwable: Warning: You did not close the PDF Document

这个问题很烦人,从第三方类库例出来。

在网上记录下找到的解决办法:

原来的代码:

StringBuffer content = new StringBuffer(“”); //   内容的所述文件 
的FileInputStream FIS = 新的FileInputStream(F);
                        PDFParser p = 新的 PDFParser(fis);
                        p.parse();
                        PDFTextStripper ts = new PDFTextStripper();
                        content.append(ts.getText(p.getPDDocument()));

不报错的代码:

StringBuffer content = new StringBuffer(“”); //   内容的所述文件 
                PDDocument pdfDocument = 空 ;
                尝试 {
                        FileInputStream fis = new FileInputStream(f);
                        PDFTextStripper stripper = new PDFTextStripper();
                        pdfDocument = PDDocument.load(fis);
                        StringWriter writer = new StringWriter();
                        stripper.writeText(pdfDocument,writer);
                        content.append(writer.getBuffer()的toString());
                        fis.close();
                } catch(java.io.IOException e){
                        System.err.println(“IOException =” + e);
                        System.exit(1);
                } finally {
                         if(pdfDocument!= null){
 //                               System.err.println(“关闭文档” + f + “...”);
                                org.pdfbox.cos.COSDocument cos = pdfDocument.getDocument();
                                cos.close();
//                               System.err.println(“Closed” + cos);
                                pdfDocument.close();
                        }
                }














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值