Java讀取PDF中的表格的方法示例
一、概述
本文以Java示例展示讀取PDF中的表格的方法。這里導(dǎo)入Spire.PDF for Javah中的jar包,并使用其提供的相關(guān)及方法來實(shí)現(xiàn)獲取表格中的文本內(nèi)容。下表中整理了本次代碼使用到的主要類、方法及解釋,供參考:
類型 | 描述 |
PdfDocument Class | Represents a pdf document model. |
PdfDocument. loadFromFile (string filename) Method | Loads a PDF document. |
PdfTableExtractor Class | Represents the PDF table extractor. |
PdfTable Class | Defines a PDF table. |
PdfTableExtractor. extractTable (int pageIndex) Method | Extracts table from page. |
PdfTable.getText(int rowIndex,int columnIndex) Method | Gets Text in cell. |
FileWriter. write() Method | Saves extracted text in table to a .txt file. |
二、環(huán)境配置
- IntelliJ IDEA 2018(JDK 1.8.0)
- PDF 測(cè)試文檔
- PDF Jar包:Spire.PDF for Java Version: 4.10.2
Jar包的兩種導(dǎo)入方法:
1. 手動(dòng)導(dǎo)入
將jar包下載到本地,解壓。然后執(zhí)行如下步驟來手動(dòng)導(dǎo)入:
2. Maven倉庫下載導(dǎo)入
如果使用maven,需在pom.xml中配置maven路徑,指定依賴,如下:
<repositories> <repository> <id>com.e-iceblue</id> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>4.10.2</version> </dependency> </dependencies>
三、讀取PDF中的表格
import com.spire.pdf.*; import com.spire.pdf.utilities.PdfTable; import com.spire.pdf.utilities.PdfTableExtractor; import java.io.FileWriter; import java.io.IOException; public class ExtractTable { public static void main(String[] args)throws IOException { //加載PDF文檔 PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("test.pdf"); //創(chuàng)建StringBuilder類的實(shí)例 StringBuilder builder = new StringBuilder(); //抽取表格 PdfTableExtractor extractor = new PdfTableExtractor(pdf); PdfTable[] tableLists ; for (int page = 0; page < pdf.getPages().getCount(); page++) { tableLists = extractor.extractTable(page); if (tableLists != null && tableLists.length > 0) { for (PdfTable table : tableLists) { int row = table.getRowCount(); int column = table.getColumnCount(); for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { String text = table.getText(i, j); builder.append(text+" "); } builder.append("\r\n"); } } } } //將提取的表格內(nèi)容寫入txt文檔 FileWriter fileWriter = new FileWriter("ExtractedTable.txt"); fileWriter.write(builder.toString()); fileWriter.flush(); fileWriter.close(); } }
表格內(nèi)容讀取結(jié)果:
注意事項(xiàng):
1. 注意使用的PDF Jar包版本為4.10.2,低于此版本的jar包不支持讀取表格;
2. 代碼中的文件路徑為 F:\IDEAProject\Table_PDF\test.pdf 和 F:\IDEAProject\Table_PDF\ExtractedTable.txt , 文件路徑可自定義為其他路徑。
到此這篇關(guān)于Java讀取PDF中的表格的方法示例的文章就介紹到這了,更多相關(guān)Java讀取PDF表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java設(shè)計(jì)模式-代理模式(實(shí)例講解)
下面小編就為大家?guī)硪黄猨ava設(shè)計(jì)模式-代理模式(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09如何使用Bean Validation 解決業(yè)務(wù)中參數(shù)校驗(yàn)
這篇文章主要介紹了如何使用Bean Validation 解決業(yè)務(wù)中參數(shù)校驗(yàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07詳細(xì)聊聊Spring MVC重定向與轉(zhuǎn)發(fā)
大家應(yīng)該都知道請(qǐng)求重定向和請(qǐng)求轉(zhuǎn)發(fā)都是web開發(fā)中資源跳轉(zhuǎn)的方式,這篇文章主要給大家介紹了關(guān)于Spring MVC重定向與轉(zhuǎn)發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09基于Freemarker和xml實(shí)現(xiàn)Java導(dǎo)出word
這篇文章主要介紹了基于Freemarker和xml實(shí)現(xiàn)Java導(dǎo)出word,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java 用Prometheus搭建實(shí)時(shí)監(jiān)控系統(tǒng)過程詳解
這篇文章主要介紹了用Prometheus搭建實(shí)時(shí)監(jiān)控系統(tǒng)過程詳解之上帝之火,普羅米修斯的崛起,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Mybatis如何傳入多個(gè)參數(shù)(實(shí)體類型和基本類型)
這篇文章主要介紹了Mybatis如何傳入多個(gè)參數(shù)(實(shí)體類型和基本類型),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06