java提取pdf對(duì)應(yīng)的目錄及頁(yè)碼實(shí)踐
java提取pdf對(duì)應(yīng)目錄及頁(yè)碼
添加依賴
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
代碼
public static void main(String[] args) throws IOException {
// File file = new File("E:\\navicat.pdf");
File file = new File("C:\\Users\\msi\\Desktop\\max.pdf");
PDDocument document = PDDocument.load(file);
// 獲取PDF文件的結(jié)構(gòu)樹根對(duì)象
// PDStructureTreeRoot structureTree = document.getDocumentCatalog().getStructureTreeRoot();
// 獲取PDF文件的文檔目錄對(duì)象
PDDocumentOutline documentOutline = document.getDocumentCatalog().getDocumentOutline();
// 輸出目錄內(nèi)容
if (documentOutline != null) {
printOutline(documentOutline, "",0);
}
// 創(chuàng)建PDFTextStripper對(duì)象,用于提取文本
// PDFTextStripper pdfStripper = new PDFTextStripper();
// // 提取文本
// String text = pdfStripper.getText(document);
// // 輸出文本內(nèi)容
// System.out.println(text);
// 關(guān)閉PDDocument對(duì)象
document.close();
}
// 遞歸輸出目錄內(nèi)容
private static void printOutline(PDOutlineNode documentOutline, String indent,int i) throws IOException {
PDOutlineItem item = documentOutline.getFirstChild();
i++;
indent = indent + " ";
while (item != null){
// PDPageDestination destination = (PDPageDestination) item.getDestination();
// int pageNumber = destination.retrievePageNumber();
int pages = 0;
if(item.getDestination() instanceof PDPageDestination){
PDPageDestination pd = (PDPageDestination) item.getDestination();
pages = pd.retrievePageNumber() + 1;
}
if (item.getAction() instanceof PDActionGoTo) {
PDActionGoTo gta = (PDActionGoTo) item.getAction();
if (gta.getDestination() instanceof PDPageDestination) {
PDPageDestination pd = (PDPageDestination) gta.getDestination();
pages = pd.retrievePageNumber() + 1;
}
}
System.out.println("------" +indent + item.getTitle() + "----"+pages+" 層級(jí):"+i );
// 遞歸處理子項(xiàng)
printOutline(item, indent ,i);
// 獲取同級(jí)
item= item.getNextSibling();
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot項(xiàng)目構(gòu)建時(shí)各種依賴詳細(xì)介紹與依賴關(guān)系說(shuō)明詳解
SpringBoot通過(guò)spring-boot-dependencies統(tǒng)一依賴版本管理,spring-boot-starter-web簡(jiǎn)化Web開發(fā)流程,自動(dòng)配置核心功能,提升開發(fā)效率與兼容性,下面通過(guò)本文介紹Springboot項(xiàng)目構(gòu)建時(shí)各種依賴詳細(xì)介紹與依賴關(guān)系說(shuō)明,感興趣的朋友一起看看吧2025-08-08
Java Swing組件JFileChooser用法實(shí)例分析
這篇文章主要介紹了Java Swing組件JFileChooser用法,結(jié)合實(shí)例形式分析了java Swing組件JFileChooser文件選擇器的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-11-11
ShardingSphere如何進(jìn)行sql重寫示例詳解
這篇文章主要為大家介紹了ShardingSphere如何進(jìn)行sql重寫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
舉例分析Python中設(shè)計(jì)模式之外觀模式的運(yùn)用
這篇文章主要介紹了Python中設(shè)計(jì)模式之外觀模式的運(yùn)用,外觀模式主張以分多模塊進(jìn)行代碼管理而減少耦合,需要的朋友可以參考下2016-03-03
Spring導(dǎo)入properties配置文件代碼示例
這篇文章主要介紹了Spring導(dǎo)入properties配置文件代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Ajax 驗(yàn)證用戶輸入的驗(yàn)證碼是否與隨機(jī)生成的一致
本文主要介紹了后臺(tái)驗(yàn)證用戶輸入的驗(yàn)證碼是否與隨機(jī)生成的驗(yàn)證碼一致的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
超簡(jiǎn)潔java實(shí)現(xiàn)雙色球若干注隨機(jī)號(hào)碼生成(實(shí)例代碼)
這篇文章主要介紹了超簡(jiǎn)潔java實(shí)現(xiàn)雙色球若干注隨機(jī)號(hào)碼生成(實(shí)例代碼),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
java學(xué)習(xí)之junit單元測(cè)試案例(經(jīng)典版)
這篇文章主要介紹了java學(xué)習(xí)之junit單元測(cè)試的相關(guān)資料,文中講解了JUnit單元測(cè)試的基本概念、作用、使用assert進(jìn)行驗(yàn)證、覆蓋率分析、BeforeEach和AfterAll的使用、通過(guò)反射和注解實(shí)現(xiàn)測(cè)試,需要的朋友可以參考下2024-12-12

