JAVA PDF操作之實現(xiàn)截取N頁和多個PDF合并
JAVA PDF 截取N頁,生成新文件,轉(zhuǎ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頁結(jié)束
*/
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轉(zhuǎn)圖片
*
* @param pdfFile PDF 文件
* @param imageFile 輸出的圖片文件
* @param from 開始頁 從1開始
* @param end 結(jié)束頁 最大為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();
//只取一頁,等于傳進(jìn)來的名稱,多頁時,加上 頁號
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));
// 打開文檔準(zhǔn)備寫入內(nèi)容
document.open();
// 將第一個 PDF 的所有頁面復(fù)制到輸出 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");
}到此這篇關(guān)于JAVA PDF操作之實現(xiàn)截取N頁和多個PDF合并的文章就介紹到這了,更多相關(guān)JAVA PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中關(guān)于getProperties方法的使用
這篇文章主要介紹了java中關(guān)于getProperties方法的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java接口回調(diào)和方法回調(diào)的簡單實現(xiàn)步驟
這篇文章主要介紹了Java接口回調(diào)和方法回調(diào)的相關(guān)資料,接口回調(diào)是一種設(shè)計模式,實現(xiàn)三方解耦,調(diào)用者提供接口實現(xiàn),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
關(guān)于注解式的分布式Elasticsearch的封裝案例
這篇文章主要介紹了關(guān)于注解式的分布式Elasticsearch的封裝案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
SpringBoot-JWT生成Token和攔截器的使用(訪問受限資源)
本文主要介紹了SpringBoot-JWT生成Token和攔截器的使用(訪問受限資源),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Mybatis中一對多(collection)和一對一(association)的組合查詢使用
這篇文章主要介紹了Mybatis中一對多(collection)和一對一(association)的組合查詢使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內(nèi)容的方法
這篇文章主要介紹了JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內(nèi)容的方法,實例分析了java爬蟲的兩種實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07

