使用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();
                        }
                }














  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Apache PDFBox 库来提取 PDF 文件中的内容。下面是一个简单的例子,演示如何使用 PDFBox 提取 PDF 文件中的文本内容: ```java import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; public class PdfBoxTest { public static void main(String[] args) throws IOException { // 加载 PDF 文件 PDDocument document = PDDocument.load(new File("test.pdf")); // 创建一个 PDFTextStripper 对象 PDFTextStripper stripper = new PDFTextStripper(); // 获取 PDF 文件的文本内容 String text = stripper.getText(document); // 输出文本内容 System.out.println(text); // 关闭 PDF 文件 document.close(); } } ``` 运行该程序,可以得到 PDF 文件中的文本内容。 当然,如果需要提取 PDF 文件中的其它内容,比如图片、表格等,也可以使用 PDFBox 提供的相应工具类来实现。例如,使用 PDFBox 提取 PDF 文件中的图片,可以使用如下代码: ```java import java.io.File; import java.io.IOException; import java.util.List; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.rendering.PDFRenderer; public class PdfBoxTest { public static void main(String[] args) throws IOException { // 加载 PDF 文件 PDDocument document = PDDocument.load(new File("test.pdf")); // 创建 PDFRenderer 对象 PDFRenderer renderer = new PDFRenderer(document); // 获取 PDF 文件中的所有页面 List<PDPage> pages = document.getPages(); // 遍历所有页面,提取图片 for (int i = 0; i < pages.size(); i++) { PDPage page = pages.get(i); int pageNum = i + 1; String fileName = "page" + pageNum + ".png"; renderer.renderImageWithDPI(pageNum - 1, 300, fileName); } // 关闭 PDF 文件 document.close(); } } ``` 运行该程序,可以将 PDF 文件中的所有页面转换成 PNG 图片保存到本地。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值