JAVA PDF操作之實現(xiàn)截取N頁和多個PDF合并
更新時間:2025年01月16日 10:27:14 作者:VipSoft
這篇文章主要為大家詳細介紹了java關于PDF的一些操作,例如截取N頁并生成新文件,轉圖片以及多個PDF合并,文中的示例代碼講解詳細,感興趣的可以了解下
JAVA PDF 截取N頁,生成新文件,轉圖片,多個PDF 合并
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
完整代碼
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class PdfUtil {
/**
* 截取pdfFile的第from頁至第end頁,組成一個新的文件名
*
* @param pdfFile 要切割的pdf文件
* @param newFile 切割后形成的新的pdf文件
* @param from 從第N頁開始
* @param end 到第N頁結束
*/
public static void partitionPdf(String pdfFile, String newFile, int from, int end) {
Document document = null;
PdfCopy copy = null;
PdfReader reader = null;
try {
reader = new PdfReader(pdfFile);
int pageCount = reader.getNumberOfPages();
if (from < 1) {
from = 1;
}
if (from > pageCount) {
from = pageCount;
}
if (end == 0 || end > pageCount) {
end = pageCount;
}
document = new Document(reader.getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(newFile));
document.open();
for (int j = from; j <= end; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document != null) {
document.close();
}
if (copy != null) {
copy.close();
}
if (reader != null) {
reader.close();
}
}
}
/**
* pdf轉圖片
*
* @param pdfFile PDF 文件
* @param imageFile 輸出的圖片文件
* @param from 開始頁 從1開始
* @param end 結束頁 最大為PDF總頁數(shù)
* @throws Exception
*/
public static void pdfToImage(String pdfFile, String imageFile, int from, int end) throws Exception {
PDDocument doc = null;
ByteArrayOutputStream os = null;
InputStream stream = null;
OutputStream out = null;
try {
//pdf路徑
stream = new FileInputStream(pdfFile);
// 加載解析PDF文件
doc = PDDocument.load(stream);
PDFRenderer pdfRenderer = new PDFRenderer(doc);
PDPageTree pages = doc.getPages();
int pageCount = pages.getCount();
if (from < 1) {
from = 1;
}
if (from > pageCount) {
from = pageCount;
}
if (end == 0 || end > pageCount) {
end = pageCount;
}
for (int i = from; i <= end; i++) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(i - 1, 200); //PDFBOX 是從0開始的,from初始值為1,所以這邊要減 i-1
os = new ByteArrayOutputStream();
ImageIO.write(bim, "jpg", os);
byte[] dataList = os.toByteArray();
//只取一頁,等于傳進來的名稱,多頁時,加上 頁號
String imageFilePath = from == end ? saveImgFile : saveImgFile.replace(".jpg", "_" + i + ".jpg");
File file = new File(imageFilePath);
if (!file.getParentFile().exists()) {
// 不存在則創(chuàng)建父目錄及子文件
file.getParentFile().mkdirs();
file.createNewFile();
}
out = new FileOutputStream(file);
out.write(dataList);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (doc != null) {
doc.close();
}
if (os != null) {
os.close();
}
if (stream != null) {
stream.close();
}
if (out != null) {
out.close();
}
}
}
//多個PDF合并成一個
public static void mergePDFFiles(List<String> pdfFiles, String outputPdf) throws IOException {
// 創(chuàng)建一個新的 PDF 閱讀器對象和一個新的 PDF 寫入對象
PdfReader reader = null;
PdfCopy copy = null;
Document document = new Document();
try {
// 創(chuàng)建 PDF 閱讀器對象和寫入對象
reader = new PdfReader(pdfFiles.get(0));
copy = new PdfCopy(document, new FileOutputStream(outputPdf));
// 打開文檔準備寫入內容
document.open();
// 將第一個 PDF 的所有頁面復制到輸出 PDF 中
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
PdfImportedPage page = copy.getImportedPage(reader, i);
copy.addPage(page);
}
// 將其它PDF的所有頁,輸出到 PDF 中
for (int i = 1; i < pdfFiles.size(); i++) {
reader = new PdfReader(pdfFiles.get(i));
for (int j = 1; j <= reader.getNumberOfPages(); j++) {
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document != null) {
document.close();
}
if (copy != null) {
copy.close();
}
if (reader != null) {
reader.close();
}
}
}
}
測試類
@Test
void pdf() throws Exception {
String pdfFile = "D:\\Desktop\\20220117.pdf";
String jpgFile = "D:\\Desktop\\20220117.jpg";
PdfUtil.pdfToImage(pdfFile, jpgFile, 1, 1);
}
@Test
void testMerge() throws IOException {
List<String> pdfFiles = new ArrayList<>();
pdfFiles.add("D:\\Projects\\20231225180735.pdf");
pdfFiles.add("D:\\Projects\\20231225182535.pdf");
pdfFiles.add("D:\\Projects\\20231225184135.pdf");
PdfUtil.mergePDFFiles(pdfFiles, "D:\\Projects\\New.pdf");
}到此這篇關于JAVA PDF操作之實現(xiàn)截取N頁和多個PDF合并的文章就介紹到這了,更多相關JAVA PDF內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java發(fā)送http請求并獲取狀態(tài)碼的簡單實例
下面小編就為大家?guī)硪黄猨ava發(fā)送http請求并獲取狀態(tài)碼的簡單實例。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05
idea hibernate jpa 生成實體類的實現(xiàn)
這篇文章主要介紹了idea hibernate jpa 生成實體類的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
SpringBoot中注解實現(xiàn)定時任務的兩種方式
這篇文章主要介紹了SpringBoot中注解實現(xiàn)定時任務的兩種方式,SpringBoot 定時任務是一種在SpringBoot應用中自動執(zhí)行任務的機制,通過使用Spring框架提供的@Scheduled注解,我們可以輕松地創(chuàng)建定時任務,需要的朋友可以參考下2023-10-10
Mybatis-plus配置分頁插件返回統(tǒng)一結果集
本文主要介紹了Mybatis-plus配置分頁插件返回統(tǒng)一結果集,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06
springboot的LogbackLoggingSystem配置加載流程解析
這篇文章主要介紹了springboot的LogbackLoggingSystem配置加載流程源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

