java PDF添加圖層的方法 支持多頁(yè)圖層添加
更新時(shí)間:2018年02月01日 09:58:50 作者:漂流的老妖怪
這篇文章主要為大家詳細(xì)介紹了java PDF添加圖層的方法,支持多頁(yè)圖層添加,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
java PDF添加圖層,支持多頁(yè)圖層添加,具體如下
代碼:
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ù)字(按照?qǐng)D片名稱數(shù)字順序合成在PDF對(duì)應(yīng)頁(yè)數(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頁(yè)數(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(".")); // 圖片名稱(對(duì)應(yīng)頁(yè)數(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;
}
}
測(cè)試:
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:



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 對(duì)ArrayList進(jìn)行分頁(yè)實(shí)例代碼
這篇文章主要介紹了java 對(duì)ArrayList進(jìn)行分頁(yè)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
Java正則表達(dá)式如何匹配特定html標(biāo)簽內(nèi)的內(nèi)容
這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式如何匹配特定html標(biāo)簽內(nèi)的內(nèi)容的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java由淺入深細(xì)數(shù)數(shù)組的操作下
數(shù)組對(duì)于每一門編程語(yǔ)言來(lái)說(shuō)都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語(yǔ)言對(duì)數(shù)組的實(shí)現(xiàn)及處理也不盡相同。Java?語(yǔ)言中提供的數(shù)組是用來(lái)存儲(chǔ)固定大小的同類型元素2022-04-04
java異常:異常處理--try-catch結(jié)構(gòu)詳解
今天小編就為大家分享一篇關(guān)于Java異常處理之try...catch...finally詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2021-09-09
MyBatis主鍵生成策略中useGeneratedKeys和<selectKey>的區(qū)別
本文主要介紹了MyBatis主鍵生成策略中useGeneratedKeys和<selectKey>的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01

