java中如何解析pdf文件
发表于:2025-02-16 作者:千家信息网编辑
千家信息网最后更新 2025年02月16日,本篇文章给大家分享的是有关java中如何解析pdf文件,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。第一种 使用开源组织提供的开源框架
千家信息网最后更新 2025年02月16日java中如何解析pdf文件
本篇文章给大家分享的是有关java中如何解析pdf文件,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
第一种 使用开源组织提供的开源框架 pdfbox
api : https://pdfbox.apache.org/
特点:免费,功能强大,解析中文或许会存在乱码,格式有点乱,没有国产解析的那么美化。
可以按照指定的模板,对pdf进行修改添加删除等操作,总之操作很骚,很强大。
1 pdfbox 需要带入依赖
org.apache.pdfbox pdfbox 2.0.15 org.apache.pdfbox fontbox 2.0.15 org.apache.pdfbox jempbox 1.8.16
2 代码
/** * 功能 PDF读写类 * @CreateTime 2011-4-14 下午02:44:11 */public class PDFUtil { // public static final String CHARACTOR_FONT_CH_FILE = "SIMFANG.TTF"; //仿宋常规 public static final String CHARACTOR_FONT_CH_FILE = "SIMHEI.TTF"; //黑体常规 public static final Rectangle PAGE_SIZE = PageSize.A4; public static final float MARGIN_LEFT = 50; public static final float MARGIN_RIGHT = 50; public static final float MARGIN_TOP = 50; public static final float MARGIN_BOTTOM = 50; public static final float SPACING = 20; private Document document = null; /** * 功能:创建导出数据的目标文档 * @param fileName 存储文件的临时路径 * @return */ public void createDocument(String fileName) { File file = new File(fileName); FileOutputStream out = null; document = new Document(PAGE_SIZE, MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM); try { out = new FileOutputStream(file);// PdfWriter writer = PdfWriter.getInstance(document, out); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } // 打开文档准备写入内容 document.open(); } /** * 将章节写入到指定的PDF文档中 * @param chapter * @return */ public void writeChapterToDoc(Chapter chapter) { try { if(document != null) { if(!document.isOpen()) document.open(); document.add(chapter); } } catch (DocumentException e) { e.printStackTrace(); } } /** * 功能 创建PDF文档中的章节 * @param title 章节标题 * @param chapterNum 章节序列号 * @param alignment 0表示align=left,1表示align=center * @param numberDepth 章节是否带序号 设值=1 表示带序号 1.章节一;1.1小节一...,设值=0表示不带序号 * @param font 字体格式 * @return Chapter章节 */ public static Chapter createChapter(String title, int chapterNum, int alignment, int numberDepth, Font font) { Paragraph chapterTitle = new Paragraph(title, font); chapterTitle.setAlignment(alignment); Chapter chapter = new Chapter(chapterTitle, chapterNum); chapter.setNumberDepth(numberDepth); return chapter; } /** * 功能:创建某指定章节下的小节 * @param chapter 指定章节 * @param title 小节标题 * @param font 字体格式 * @param numberDepth 小节是否带序号 设值=1 表示带序号 1.章节一;1.1小节一...,设值=0表示不带序号 * @return section在指定章节后追加小节 */ public static Section createSection(Chapter chapter, String title, Font font, int numberDepth) { Section section = null; if(chapter != null) { Paragraph sectionTitle = new Paragraph(title, font); sectionTitle.setSpacingBefore(SPACING); section = chapter.addSection(sectionTitle); section.setNumberDepth(numberDepth); } return section; } /** * 功能:向PDF文档中添加的内容 * @param text 内容 * @param font 内容对应的字体 * @return phrase 指定字体格式的内容 */ public static Phrase createPhrase(String text,Font font) { Phrase phrase = new Paragraph(text,font); return phrase; } /** * 功能:创建列表 * @param numbered 设置为 true 表明想创建一个进行编号的列表 * @param lettered 设置为true表示列表采用字母进行编号,为false则用数字进行编号 * @param symbolIndent * @return list */ public static List createList(boolean numbered, boolean lettered, float symbolIndent) { List list = new List(numbered, lettered, symbolIndent); return list; } /** * 功能:创建列表中的项 * @param content 列表项中的内容 * @param font 字体格式 * @return listItem */ public static ListItem createListItem(String content, Font font) { ListItem listItem = new ListItem(content, font); return listItem; } /** * 功能:创造字体格式 * @param fontname * @param size 字体大小 * @param style 字体风格 * @param color 字体颜色 * @return Font */ public static Font createFont(String fontname, float size, int style, BaseColor color) { Font font = FontFactory.getFont(fontname, size, style, color); return font; } /** * 功能: 返回支持中文的字体---仿宋 * @param size 字体大小 * @param style 字体风格 * @param color 字体 颜色 * @return 字体格式 */ public static Font createCHineseFont(float size, int style, BaseColor color) { BaseFont bfChinese = null; try { bfChinese = BaseFont.createFont(CHARACTOR_FONT_CH_FILE,BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return new Font(bfChinese, size, style, color); } /** * 最后关闭PDF文档 */ public void closeDocument() { if(document != null) { document.close(); } } /** * 读PDF文件,使用了pdfbox开源项目 * @param fileName */ public static void readPDF(String fileName) { File file = new File(fileName); FileInputStream in = null; try { in = new FileInputStream(fileName); // 新建一个PDF解析器对象 PDFParser parser = new PDFParser(new RandomAccessFile(file,"rw")); // 对PDF文件进行解析 parser.parse(); // 获取解析后得到的PDF文档对象 PDDocument pdfdocument = parser.getPDDocument(); // 新建一个PDF文本剥离器 PDFTextStripper stripper = new PDFTextStripper(); // 从PDF文档对象中剥离文本 String result = stripper.getText(pdfdocument); FileWriter fileWriter = new FileWriter(new File("pdf.txt")); fileWriter.write(result); fileWriter.flush(); fileWriter.close(); System.out.println("PDF文件的文本内容如下:"); System.out.println(result); } catch (Exception e) { System.out.println("读取PDF文件" + file.getAbsolutePath() + "生失败!" + e); e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } } } /** * 测试pdf文件的创建 * @param args */ public static void main(String[] args) { String fileName = "C:\Users\tizzy\Desktop\测试.pdf"; //这里先手动把绝对路径的文件夹给补上。 PDFUtil pdfUtil = new PDFUtil(); pdfUtil.writeChapterToDoc(chapter); pdfUtil.closeDocument(); }}
第二种 使用国产的框架 Spire.PDF
包含两种版本
1 免费版
https://www.e-iceblue.cn/Downloads/Free-Spire-PDF-JAVA.html
2 商业版本
https://www.e-iceblue.cn/Introduce/Spire-PDF-JAVA.html
api
http://e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html
1 仓库地址 和 依赖
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/
e-iceblue spire.pdf.free 2.2.2
2 代码
public static void main(String[] args) { // 创建PdfDocument实例 PdfDocument doc = new PdfDocument(); // 加载PDF文件 doc.loadFromFile("D:\\JAVA核心知识点整理.pdf"); StringBuilder sb = new StringBuilder(); PdfPageBase page; int totalPageCount = doc.getPages().getCount(); // 遍历PDF页面,获取文本 for (int i = 0; i < totalPageCount; i++) { page = doc.getPages().get(i); System.out.println("pageNo:" + i); sb.append(page.extractText(true)); } FileWriter writer; try { // 将文本写入文本文件 writer = new FileWriter("ExtractText.txt"); writer.write(sb.toString()); writer.flush(); } catch (IOException e) { e.printStackTrace(); } doc.close(); }
第三种 使用iTika 进行解析pdf
api : https://tika.apache.org/
对中文支持不是很友好,解析的格式和pdfbox类似
1 依赖
org.apache.tika tika-core 1.20
2 代码
public static String getPdfFileText(String fileName) throws IOException { PdfReader reader = new PdfReader(fileName); PdfReaderContentParser parser = new PdfReaderContentParser(reader); StringBuffer buff = new StringBuffer(); TextExtractionStrategy strategy; for (int i = 1; i <= reader.getNumberOfPages(); i++) { strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); buff.append(strategy.getResultantText()); } return buff.toString(); }
以上就是java中如何解析pdf文件,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
字体
文件
章节
功能
文档
格式
内容
小节
序号
文本
代码
对象
知识
中文
强大
国产
大小
常规
更多
标题
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
民航网络安全预测
2018互联网科技大会
外国服务器都有哪些
大武口区智能网站服务器
极品飞车19服务器
黑豆网络技术有限公司
国内价格最实惠的云服务器
数据库系统恢复策略和方法
计算机网络技术网络软件
海南服务器托管公司云服务器
什么是sql数据库系统
计算机网络技术是文科
新建一个orcl数据库
网安总队网络技术公安
关闭X11服务器
深圳华辰软件开发 培训
软件开发建网站
牡丹江软件开发收费
四级没过参加校招软件开发
我的世界创造系列服务器
网络安全受益龙头
今天光明日报网络安全
服务器数据库2012安装
添加wifi网络安全类型
网络安全隔离业绩
计算机网络技术论述题
网络安全防护情况报告
广州创梦星空网络技术有限公司
存在严重网络安全风险隐患
access数据库窗体空间顺序