itextpdf提取PDF文件中的任意頁碼實(shí)現(xiàn)示例
需求
有一個(gè)幾十頁的PDF文件,現(xiàn)在需要從中拆分出指定的頁碼,然后生成一個(gè)新的PDF文件。
這個(gè)時(shí)候,可以使用開源的 itextpdf 庫來實(shí)現(xiàn),itextpdf
的官方 github 地址為:https://github.com/itext/itextpdf.
下面通過具體的代碼來演示。
引入依賴
目前 itextpdf
最新版本為 5.5.13.3
,可以在 https://search.maven.org/ 網(wǎng)站進(jìn)行搜索。
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13.3</version> </dependency>
代碼實(shí)現(xiàn)
指定頁碼抽取
package com.magic.itextpdf; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Objects; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfSmartCopy; /** * PDF工具類 */ public class PdfUtils { /** * 抽取PDF文件 * @param sourceFile 源PDF文件路徑 * @param targetFile 目標(biāo)PDF文件路徑 * @param extractedPageNums 需要抽取的頁碼 */ public static void extract(String sourceFile, String targetFile, List<Integer> extractedPageNums) { Objects.requireNonNull(sourceFile); Objects.requireNonNull(targetFile); PdfReader reader = null; Document document = null; FileOutputStream outputStream = null; try { // 讀取源文件 reader = new PdfReader(sourceFile); // 創(chuàng)建新的文檔 document = new Document(); // 創(chuàng)建目標(biāo)PDF文件 outputStream = new FileOutputStream(targetFile); PdfCopy pdfCopy = new PdfSmartCopy(document, outputStream); // 獲取源文件的頁數(shù) int pages = reader.getNumberOfPages(); document.open(); // 注意此處的頁碼是從1開始 for (int page = 1; page <= pages; page++) { // 如果是指定的頁碼,則進(jìn)行復(fù)制 if (extractedPageNums.contains(page)) { pdfCopy.addPage(pdfCopy.getImportedPage(reader, page)); } } } catch (IOException | DocumentException e) { e.printStackTrace(); } finally { if (reader != null) { reader.close(); } if (document != null) { document.close(); } if (outputStream != null) { try { outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
extract()
方法有三個(gè)參數(shù),分包是源PDF文件路徑、目標(biāo)PDF文件路徑和指定頁碼,其中指定頁碼采用List集合進(jìn)行傳遞,比如需要抽取第1頁,可以像下面這樣調(diào)用
PdfUtils.extract("D:\\Test\\test.pdf", "D:\\Test\\test_out.pdf", Collections.singletonList(1));
如果同時(shí)需要抽取多頁,比如1、3、5頁,那么可以這樣調(diào)用
PdfUtils.extract("D:\\Test\\test.pdf", "D:\\Test\\test_out.pdf", Arrays.asList(1, 3, 5));
當(dāng)然,如果一個(gè)PDF有一百多頁,現(xiàn)在需要抽取10-60頁,如果還是像上面一樣傳遞參數(shù),則會(huì)非常麻煩,此時(shí)就可以重載一個(gè)方法,實(shí)現(xiàn)傳遞起始頁碼和結(jié)束頁碼來抽取了。
起始結(jié)束頁碼抽取
重載 extract
方法,具體的代碼如下:
/** * 抽取PDF文件 * @param sourceFile 源PDF文件路徑 * @param targetFile 目標(biāo)PDF文件路徑 * @param fromPageNum 起始頁碼 * @param toPageNum 結(jié)束頁碼 */ public static void extract(String sourceFile, String targetFile, int fromPageNum, int toPageNum) { Objects.requireNonNull(sourceFile); Objects.requireNonNull(targetFile); PdfReader reader = null; Document document = null; FileOutputStream outputStream = null; try { // 讀取源文件 reader = new PdfReader(sourceFile); // 創(chuàng)建新的文檔 document = new Document(); // 創(chuàng)建目標(biāo)PDF文件 outputStream = new FileOutputStream(targetFile); PdfCopy pdfCopy = new PdfSmartCopy(document, outputStream); // 獲取源文件的頁數(shù) int pages = reader.getNumberOfPages(); document.open(); // 注意此處的頁碼是從1開始 for (int page = 1; page <= pages; page++) { if (page >= fromPageNum && page <= toPageNum) { pdfCopy.addPage(pdfCopy.getImportedPage(reader, page)); } } } catch (IOException | DocumentException e) { e.printStackTrace(); } finally { if (reader != null) { reader.close(); } if (document != null) { document.close(); } if (outputStream != null) { try { outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
對(duì)于連續(xù)頁碼而言,這個(gè)方式更加簡單,比如要抽取 10-60頁,那么可以這樣調(diào)用
PdfUtils.extract("D:\\Test\\test.pdf", "D:\\Test\\test_out.pdf", 10, 60);
測試驗(yàn)證
現(xiàn)在有一個(gè)總共2頁的PDF文件,分別使用上面的方法進(jìn)行抽取拆分第1頁,代碼如下:
package com.magic.itextpdf; import java.util.Collections; public class Test { public static void main(String[] args) { PdfUtils.extract("D:\\Test\\test.pdf", "D:\\Test\\test_out_1.pdf", Collections.singletonList(1)); PdfUtils.extract("D:\\Test\\test.pdf", "D:\\Test\\test_out_2.pdf", 1, 1); } }
運(yùn)行后,分別生成了 test_out_1.pdf
和 test_out_2.pdf
兩個(gè)新文件,新文件都是源文件的第一頁。
其他方法
如果只是處理單個(gè)PDF文件的話,那么可以使用WPS的打印功能,或者Chrome瀏覽器的打印功能都可以實(shí)現(xiàn),非常方便。
WPS打印拆分
Chrome打印拆分
以上就是itextpdf提取PDF文件中的任意頁碼實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于itextpdf提取PDF文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
idea切換分支的時(shí)候,忽略一些無用的修改設(shè)置
這篇文章主要介紹了idea切換分支的時(shí)候,忽略一些無用的修改操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02Java?ServletContext與ServletConfig接口使用教程
ServletConfig對(duì)象,叫Servlet配置對(duì)象。主要用于加載配置文件的初始化參數(shù)。我們知道一個(gè)Web應(yīng)用里面可以有多個(gè)servlet,如果現(xiàn)在有一份數(shù)據(jù)需要傳給所有的servlet使用,那么我們就可以使用ServletContext對(duì)象了2022-09-09Arrays.sort如何實(shí)現(xiàn)降序排序
這篇文章主要介紹了Arrays.sort如何實(shí)現(xiàn)降序排序問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Java 實(shí)戰(zhàn)圖書管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)圖書管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11JAVA 格式化JSON數(shù)據(jù)并保存到j(luò)son文件中的實(shí)例
這篇文章主要介紹了JAVA 格式化JSON數(shù)據(jù)并保存到j(luò)son文件中的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10shade解決mybatis包沖突問題及項(xiàng)目引用的方法
這篇文章主要介紹了shade解決mybatis包沖突問題及項(xiàng)目引用的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08java編程SpringSecurity入門原理及應(yīng)用簡介
Spring 是非常流行和成功的 Java 應(yīng)用開發(fā)框架,Spring Security 正是 Spring 家族中的成員。Spring Security 基于 Spring 框架,提供了一套 Web 應(yīng)用安全性的完整解決方案2021-09-09