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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis collection關聯(lián)查詢多個參數(shù)方式
在使用MyBatis進行關聯(lián)查詢時,往往需要根據(jù)多個參數(shù)進行查詢,例如,使用evtId和businessType作為查詢條件,同時在resultMap中配置id和businessType1作為結果映射,這種情況下,可以通過<sql>標簽定義參數(shù)模板,或者使用@Param注解指定參數(shù)名稱2024-10-10springboot下添加全局異常處理和自定義異常處理的過程解析
在spring項目中,優(yōu)雅處理異常,好處是可以將系統(tǒng)產(chǎn)生的全部異常統(tǒng)一捕獲處理,自定義的異常也由全局異常來捕獲,如果涉及到validator參數(shù)校驗器使用全局異常捕獲也是較為方便,這篇文章主要介紹了springboot下添加全局異常處理和自定義異常處理,需要的朋友可以參考下2023-12-12Apache Maven創(chuàng)建工程的實現(xiàn)示例
本文詳細介紹了如何使用Maven創(chuàng)建一個新的Java工程,包括使用maven-archetype-plugin插件、項目的基本結構和文件、構建和運行項目的方法以及常見問題的解決,Maven通過簡化項目構建和依賴管理,成為Java開發(fā)中不可或缺的工具2024-11-11基于Freemarker和xml實現(xiàn)Java導出word
這篇文章主要介紹了基于Freemarker和xml實現(xiàn)Java導出word,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04Java編程中使用JDBC API連接數(shù)據(jù)庫和創(chuàng)建程序的方法
這篇文章主要介紹了Java編程中使用JDBC API連接數(shù)據(jù)庫和創(chuàng)建程序的基本教程,JDBC是一種用于執(zhí)行SQL語句的Java API,可以為多種關系數(shù)據(jù)庫提供統(tǒng)一訪問需要的朋友可以參考下2015-12-12