Java操作itextpdf實(shí)現(xiàn)PDF添加文字,圖片和簽名
參考文章:JAVA PDF 截取N頁(yè),生成新文件,轉(zhuǎn)圖片,多個(gè)PDF 合并
itextpdf PDF 文字、圖片 簽名
<itextpdf.version>5.5.13</itextpdf.version>
<itext-asian.version>5.2.0</itext-asian.version>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>${itextpdf.version}</version>
</dependency>
<!--沒(méi)有這個(gè)的話,添加文字會(huì)報(bào)錯(cuò)-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>${itext-asian.version}</version>
</dependency>
實(shí)現(xiàn)代碼
public class PdfElementInfo {
public enum TypeEnum {
UNKNOWN,
TEXT,
IMAGE
}
/**
* 1. 文本
* 2. 圖片
*/
private int type;
private float x;
private float y;
/**
* 文本內(nèi)容
*/
private String text;
private float fontSize;
/**
* 路徑
*/
private String imgPath;
...getter & setter....
}
/**
* 添加文字水印
*
* @param inputUrl
* @param outputUrl
* @param elementInfoList
* @param pageRange -1 全部,空:第一頁(yè),1:第一頁(yè),1~3:第1、2、3頁(yè)
*/
private void addElement(String inputUrl, String outputUrl, List<PdfElementInfo> elementInfoList, String... pageRange) {
PdfStamper stamper = null;
PdfReader reader = null;
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(outputUrl), false));
reader = new PdfReader(inputUrl);
stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages() + 1;
List<Integer> pageNumList = new ArrayList<>();
if (pageRange.length == 0) {
pageNumList.add(1);
}
if (pageNumList.size() > total) {
throw new CustomException("指定頁(yè),超過(guò)了PDF文件頁(yè)數(shù)");
}
PdfContentByte content;
// BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
// "c:\\windows\\fonts\\SIMHEI.TTF" 使用windows系統(tǒng)的黑體
BaseFont base = BaseFont.createFont("C:\\windows\\fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
PdfGState gs = new PdfGState();
for (Integer pageNum : pageNumList) {
content = stamper.getOverContent(pageNum);// 在內(nèi)容上方加水印
// content = stamper.getUnderContent(pageNum);//在內(nèi)容下方加水印
gs.setFillOpacity(0.2f);
content.beginText();
//content.setTextMatrix(390, 810);
//內(nèi)容居中,橫縱坐標(biāo),偏移量
//content.showTextAligned(Element.ALIGN_CENTER, "AAAA", 179.54f, 718.62f, 0);
for (PdfElementInfo textInfo : elementInfoList.stream().filter(p -> p.getType() == 1).collect(Collectors.toList())) {
//字體大小
content.setFontAndSize(base, textInfo.getFontSize());
content.showTextAligned(Element.ALIGN_CENTER, textInfo.getText(), textInfo.getX(), textInfo.getY(), 0);
}
for (PdfElementInfo imgInfo : elementInfoList.stream().filter(p -> p.getType() == 2).collect(Collectors.toList())) {
//添加圖片
Image image = Image.getInstance(imgInfo.getImgPath());
/*
img.setAlignment(Image.LEFT | Image.TEXTWRAP);
img.setBorder(Image.BOX); img.setBorderWidth(10);
img.setBorderColor(BaseColor.WHITE); img.scaleToFit(100072);//大小
img.setRotationDegrees(-30);//旋轉(zhuǎn)
*/
//圖片的位置(坐標(biāo))
image.setAbsolutePosition(imgInfo.getX(), imgInfo.getY());
// image of the absolute,寬、高,取最值值進(jìn)行適配
image.scaleToFit(100, 100);
//image.scalePercent(imgInfo.getScalePercent());//依照比例縮放. 調(diào)整縮放,控制圖片大小
content.addImage(image);
}
content.setFontAndSize(base, 8);
content.endText();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (stamper != null) {
stamper.close();
}
//關(guān)閉打開(kāi)的原來(lái)PDF文件,不執(zhí)行reader.close()刪除不了(必須先執(zhí)行stamper.close(),否則會(huì)報(bào)錯(cuò))
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}測(cè)試類
@Test
void testSignature() throws Exception {
String inputUrl = "D:\\THOTH\\0.SyncService\\Api.22586\\Report\\Report.pdf";
//生成的文件路徑
String outputUrl = "D:\\THOTH\\0.SyncService\\Api.22586\\Report\\Report_out_" + DateUtil.current() + ".pdf";
String imageUrl = "D:\\Users\\Pictures\\R-C.png";
List<PdfElementInfo> elementInfoList = new ArrayList<>();
PdfElementInfo textInfo = new PdfElementInfo();
textInfo.setType(PdfElementInfo.TypeEnum.TEXT.ordinal());
textInfo.setFontSize(10.5f);
textInfo.setX(200f);
textInfo.setY(100f);
textInfo.setText("張三" + DateUtil.current());
elementInfoList.add(textInfo);
textInfo = new PdfElementInfo();
textInfo.setType(PdfElementInfo.TypeEnum.TEXT.ordinal());
textInfo.setFontSize(10.5f);
textInfo.setX(400f);
textInfo.setY(100f);
textInfo.setText("李四" + DateUtil.current());
elementInfoList.add(textInfo);
PdfElementInfo imgInfo = new PdfElementInfo();
imgInfo.setType(PdfElementInfo.TypeEnum.IMAGE.ordinal());
imgInfo.setX(100f);
imgInfo.setY(200f);
imgInfo.setImgPath(imageUrl);
elementInfoList.add(imgInfo);
addElement(inputUrl, outputUrl, elementInfoList);
//刪除原來(lái)的PDF文件
/*File targetTemplePDF = new File(inputPDFFilePath);
targetTemplePDF.delete();*/
}最終效果如下

到此這篇關(guān)于Java操作itextpdf實(shí)現(xiàn)PDF添加文字,圖片和簽名的文章就介紹到這了,更多相關(guān)Java itextpdf PDF添加文字圖片簽名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java的動(dòng)態(tài)代理和靜態(tài)代理及反射常用API詳解
這篇文章主要介紹了Java的動(dòng)態(tài)代理和靜態(tài)代理及反射常用API詳解,動(dòng)態(tài)代理是一種在運(yùn)行時(shí)動(dòng)態(tài)生成代理對(duì)象的技術(shù),它是一種設(shè)計(jì)模式,用于在不修改原始對(duì)象的情況下,通過(guò)代理對(duì)象來(lái)間接訪問(wèn)原始對(duì)象,并在訪問(wèn)前后執(zhí)行額外的操作,需要的朋友可以參考下2024-01-01
避免sql注入_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了避免sql注入,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
使用Java實(shí)現(xiàn)在Excel中添加動(dòng)態(tài)數(shù)組公式
動(dòng)態(tài)數(shù)組公式是?Excel?引入的一項(xiàng)重要功能,它允許用戶從單個(gè)單元格中的公式返回多個(gè)結(jié)果值,并將這些值自動(dòng)填充到與公式單元格相鄰的單元格中,本文主要介紹了如何使用Java實(shí)現(xiàn)在Excel中添加動(dòng)態(tài)數(shù)組公式,x需要的可以參考下2023-12-12
淺談java面向?qū)ο?類,封裝,this,構(gòu)造方法)
下面小編就為大家?guī)?lái)一篇淺談java面向?qū)ο?類,封裝,this,構(gòu)造方法)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Java獲取視頻時(shí)長(zhǎng)及截取幀截圖詳解
這篇文章主要介紹了Java獲取視頻時(shí)長(zhǎng)及截取幀截圖詳解,以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。,需要的朋友可以參考下2019-06-06
Java日期時(shí)間及日期相互轉(zhuǎn)換實(shí)現(xiàn)代碼
這篇文章主要介紹了Java日期時(shí)間及日期相互轉(zhuǎn)換實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
解決poi導(dǎo)出excel無(wú)法打開(kāi)文件的問(wèn)題
這篇文章主要介紹了解決poi導(dǎo)出excel無(wú)法打開(kāi)文件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
使用Java編寫導(dǎo)出不確定行數(shù)列數(shù)數(shù)據(jù)的工具類
這篇文章主要為大家詳細(xì)介紹了如何使用Java編寫導(dǎo)出不確定行數(shù)列數(shù)數(shù)據(jù)的工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

