itextpdf提取PDF文件中的任意頁碼實現(xiàn)示例
需求
有一個幾十頁的PDF文件,現(xiàn)在需要從中拆分出指定的頁碼,然后生成一個新的PDF文件。
這個時候,可以使用開源的 itextpdf 庫來實現(xiàn),itextpdf 的官方 github 地址為:https://github.com/itext/itextpdf.
下面通過具體的代碼來演示。
引入依賴
目前 itextpdf 最新版本為 5.5.13.3,可以在 https://search.maven.org/ 網(wǎng)站進行搜索。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>代碼實現(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 目標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)建目標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++) {
// 如果是指定的頁碼,則進行復(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() 方法有三個參數(shù),分包是源PDF文件路徑、目標PDF文件路徑和指定頁碼,其中指定頁碼采用List集合進行傳遞,比如需要抽取第1頁,可以像下面這樣調(diào)用
PdfUtils.extract("D:\\Test\\test.pdf", "D:\\Test\\test_out.pdf", Collections.singletonList(1));如果同時需要抽取多頁,比如1、3、5頁,那么可以這樣調(diào)用
PdfUtils.extract("D:\\Test\\test.pdf", "D:\\Test\\test_out.pdf", Arrays.asList(1, 3, 5));當(dāng)然,如果一個PDF有一百多頁,現(xiàn)在需要抽取10-60頁,如果還是像上面一樣傳遞參數(shù),則會非常麻煩,此時就可以重載一個方法,實現(xiàn)傳遞起始頁碼和結(jié)束頁碼來抽取了。
起始結(jié)束頁碼抽取
重載 extract 方法,具體的代碼如下:
/**
* 抽取PDF文件
* @param sourceFile 源PDF文件路徑
* @param targetFile 目標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)建目標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();
}
}
}
}對于連續(xù)頁碼而言,這個方式更加簡單,比如要抽取 10-60頁,那么可以這樣調(diào)用
PdfUtils.extract("D:\\Test\\test.pdf", "D:\\Test\\test_out.pdf", 10, 60);測試驗證
現(xiàn)在有一個總共2頁的PDF文件,分別使用上面的方法進行抽取拆分第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);
}
}運行后,分別生成了 test_out_1.pdf 和 test_out_2.pdf 兩個新文件,新文件都是源文件的第一頁。
其他方法
如果只是處理單個PDF文件的話,那么可以使用WPS的打印功能,或者Chrome瀏覽器的打印功能都可以實現(xiàn),非常方便。
WPS打印拆分

Chrome打印拆分

以上就是itextpdf提取PDF文件中的任意頁碼實現(xiàn)示例的詳細內(nèi)容,更多關(guān)于itextpdf提取PDF文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java?ServletContext與ServletConfig接口使用教程
ServletConfig對象,叫Servlet配置對象。主要用于加載配置文件的初始化參數(shù)。我們知道一個Web應(yīng)用里面可以有多個servlet,如果現(xiàn)在有一份數(shù)據(jù)需要傳給所有的servlet使用,那么我們就可以使用ServletContext對象了2022-09-09
Java 實戰(zhàn)圖書管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個圖書管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
JAVA 格式化JSON數(shù)據(jù)并保存到j(luò)son文件中的實例
這篇文章主要介紹了JAVA 格式化JSON數(shù)據(jù)并保存到j(luò)son文件中的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
java編程SpringSecurity入門原理及應(yīng)用簡介
Spring 是非常流行和成功的 Java 應(yīng)用開發(fā)框架,Spring Security 正是 Spring 家族中的成員。Spring Security 基于 Spring 框架,提供了一套 Web 應(yīng)用安全性的完整解決方案2021-09-09

