java PDF添加圖層的方法 支持多頁圖層添加
java PDF添加圖層,支持多頁圖層添加,具體如下
代碼:
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; public class PdfUtils { /** * PDF添加圖層 * * @param srcPdf * 原PDF文件路徑 * @param distPdf * 合成PDF輸出路徑 * @param layerPathArr * 圖層路徑列表,圖層名稱需為數(shù)字(按照圖片名稱數(shù)字順序合成在PDF對應(yīng)頁數(shù)上) * @return * @throws IOException * @throws DocumentException */ public static String markLocalImage42Dist(String srcPdf, String distPdf, List<String> layerPathArr) throws IOException, DocumentException { File srcPdfFile = new File(srcPdf); if (!srcPdfFile.exists()) { throw new IllegalArgumentException("找不到需要添加圖層的pdf文件"); } PdfReader reader = new PdfReader(srcPdf); int n = reader.getNumberOfPages(); // PDF頁數(shù) PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(distPdf)); PdfContentByte over; for (String layerPath : layerPathArr) { File layerFile = new File(layerPath); String currentPageNo = layerFile.getName().substring(0, layerFile.getName().lastIndexOf(".")); // 圖片名稱(對應(yīng)頁數(shù)) boolean isNum = currentPageNo.matches("[0-9]+"); if (!isNum) { throw new IllegalArgumentException("圖層名稱是不是數(shù)字"); } Image img = Image.getInstance(layerPath); img.setAbsolutePosition(0, 0); if (n > 0 && n >= Integer.parseInt(currentPageNo)) { over = stamp.getOverContent(Integer.parseInt(currentPageNo)); over.addImage(img); } } stamp.close(); reader.close(); return distPdf; } }
測試:
public static void main(String[] args) throws IOException, DocumentException { List<String> imgUrlList = new ArrayList<>(); imgUrlList.add("D:/ts/testPDF/1.png"); //imgUrlList.add("D:/ts/testPDF/2.png"); imgUrlList.add("D:/ts/testPDF/3.png"); markLocalImage42Dist("D:/ts/testPDF/testPDF.pdf", "D:/ts/testPDF/testPDF2.pdf", imgUrlList); }
結(jié)果:
原PDF:
合成后PDF:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入解析Andoird應(yīng)用開發(fā)中View的事件傳遞
這篇文章主要介紹了深入解析Andoird應(yīng)用開發(fā)中View的事件傳遞,其中重點(diǎn)講解了ViewGroup的事件傳遞流程,需要的朋友可以參考下2016-02-02Java synchronized關(guān)鍵字和Lock接口實(shí)現(xiàn)原理
這篇文章主要介紹了Java synchronized關(guān)鍵字和Lock接口實(shí)現(xiàn)原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12springboot創(chuàng)建的web項(xiàng)目整合Quartz框架的項(xiàng)目實(shí)踐
本文主要介紹了springboot創(chuàng)建的web項(xiàng)目整合Quartz框架的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06SpringBoot中的定時(shí)任務(wù)和異步調(diào)用詳解
這篇文章主要介紹了SpringBoot中的定時(shí)任務(wù)和異步調(diào)用詳解,SpringBoot 定時(shí)任務(wù)是一種在SpringBoot應(yīng)用中自動執(zhí)行任務(wù)的機(jī)制,通過使用Spring框架提供的@Scheduled注解,我們可以輕松地創(chuàng)建定時(shí)任務(wù),需要的朋友可以參考下2023-10-10Java使用HttpClient實(shí)現(xiàn)文件下載
這篇文章主要為大家詳細(xì)介紹了Java使用HttpClient實(shí)現(xiàn)文件下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Java httpClient連接池支持多線程高并發(fā)的實(shí)現(xiàn)
本文主要介紹了Java httpClient連接池支持多線程高并發(fā)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08MyBatisPlus 一對多、多對一、多對多的完美解決方案
這篇文章主要介紹了MyBatisPlus 一對多、多對一、多對多的完美解決方案,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11