import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStreamWriter;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class Pdftext {
public static String getTxt(File f) throws Exception {
String ts = "";
try {
String temp = "";
PDDocument pdfdocument = PDDocument.load(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(pdfdocument.getDocument(), writer);
pdfdocument.close();
out.close();
writer.close();
byte[] contents = out.toByteArray();
ts = new String(contents);
System.out.println(f.getName() + "length is:" + contents.length
+ "\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
return ts;
}
}
public static void main(String[] args) throws Exception {
File file = new File("d:/hello.pdf");
System.out.println(Pdftext.getTxt(file));
/*
File file = new File("d:/hello.pdf");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
PDFParser parser = new PDFParser(bis);
//
parser.parse();
PDDocument document = parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
String s = stripper.getText(document);
// ////////////
document.close();// /////////
bis.close();
// //////////
File ff = new File("d:/hello.pdf");
ff.createNewFile();
if (ff.exists())
{
ff.createNewFile();
}
FileWriter fw = new FileWriter(ff);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(s);
bw.close();*/
}
}
Java 解析 PDF, pdfbox读取PDF内容
5星 · 超过95%的资源 需积分: 50 25 浏览量
2019-03-24
02:59:40
上传
评论 2
收藏 3.07MB RAR 举报
weixin_38669628
- 粉丝: 383
- 资源: 6万+
最新资源
- 重启进行BIOS快捷方式,不需要开机按BIOS键
- 威纶通触摸屏编程软件Easy builder pro V6.09.01.556安装包(2024.04).txt
- WindowsAdminCenter
- 老飞飞搭建基础通用数据库V19数据库.rar
- jquery.js
- 机械设计多工位ACF贴胶带&预压设备sw18可编辑非常好的设计图纸100%好用.zip
- 基于Pytorch复现Point-Transformer,用于ShapeNet数据集点云分割
- 【医学影像分析】2D超声图像的分割检测(Ultrasound Nerve Segmentation - Kaggle数据集)
- 嘎嘎香的五款神仙谷歌插件
- .arch书源导入教程.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页