失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > itext 5.3.0实现对pdf文件添加(文字和图片)水印

itext 5.3.0实现对pdf文件添加(文字和图片)水印

时间:2019-07-26 19:22:52

相关推荐

itext 5.3.0实现对pdf文件添加(文字和图片)水印

在itext 较新的版本中, 对中文的支持还是存在着问题,在网络上得到的信息和多方尝试下,将字体文件xx.TTF放到项目里面,然后加载到BaseFont 中,可行。如下:BaseFont font = BaseFont.createFont(path + "MSYH.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

需要jar包:

官网下载的 itext-5.3.0.zip 下的jar包

itextpdf-5.3.0.jar

itext-xtra-5.3.0.jar

不需要对中文支持的扩展jar包,其由上文提到的 加载字体文件到BaseFont中来实现;

具体的加水印代码如下:

package com.sslinm.tools;import java.io.FileOutputStream;/*** 【功能描述:给PDF 加水印功能,(文字水印和图片水印)】 * 【功能详细描述:逐条详细描述功能】* * @author 【lfssay】* @see 【相关类/方法】* @version 【类版本号, -8-20 上午11:22:21】* @since 【产品/模块版本】*/public class PdfAddWaterMark {static Log log = LogFactory.getLog(PdfAddWaterMark.class);public static void main(String[] args) throws DocumentException, IOException {new PdfAddWaterMark().addWaterMark("E:/tt/1.pdf", "E:/tt/129.pdf", "国家图书馆藏", "E:/tt/1.jpg", 400, 800, 200, 800);}/*** * 【功能描述:添加图片和文字水印】 【功能详细描述:功能详细描述】* * @see 【类、类#方法、类#成员】* @param srcFile* 待加水印文件* @param destFile* 加水印后存放地址* @param text* 加水印的文本内容* @param imgFile* 加水印图片文件* @param textWidth* 文字横坐标* @param textHeight* 文字纵坐标* @param imgWidth* 图片横坐标* @param imgHeight* 图片纵坐标* @throws IOException* @throws DocumentException*/public void addWaterMark(String srcFile, String destFile, String text, String imgFile, int textWidth,int textHeight, int imgWidth, int imgHeight) throws IOException, DocumentException {// 待加水印的文件PdfReader reader = new PdfReader(srcFile);// 加完水印的文件PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));int total = reader.getNumberOfPages() + 1;PdfContentByte content;String path = this.getClass().getResource("/").getPath();// 设置字体// BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",// BaseFont.EMBEDDED);//加载字库来完成对字体的创建BaseFont font = BaseFont.createFont(path + "MSYH.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);// BaseFont base2 = BaseFont.createFont(BaseFont.HELVETICA,// BaseFont.WINANSI, BaseFont.EMBEDDED);// 水印文字String waterText = text;Image image = null;if (!StringUtils.isBlank(imgFile)) {image = Image.getInstance(imgFile);image.setAbsolutePosition(imgWidth, imgHeight);// 设置图片的显示大小image.scaleToFit(100, 125);}int j = waterText.length(); // 文字长度char c = 0;int high = 0;// 高度// 循环对每页插入水印for (int i = 1; i < total; i++) {// 水印的起始high = 50;// 水印在之前文本之上content = stamper.getOverContent(i);if (image != null) {content.addImage(image);}if (!StringUtils.isBlank(text)) {// 开始content.beginText();// 设置颜色 默认为蓝色content.setColorFill(BaseColor.BLUE);// content.setColorFill(Color.GRAY);// 设置字体及字号content.setFontAndSize(font, 38);// 设置起始位置// content.setTextMatrix(400, 880);content.setTextMatrix(textWidth, textHeight);// 开始写入水印content.showTextAligned(Element.ALIGN_LEFT, text, textWidth, textHeight, 45);// for (int k = 0; k < j; k++) {// content.setTextRise(14);// c = waterText.charAt(k);// // 将char转成字符串// content.showText(c + "");// high -= 5;// }content.endText();}}stamper.close();log.info("===" + srcFile + "===添加水印到==" + destFile + "==成功=====");}}

如果觉得《itext 5.3.0实现对pdf文件添加(文字和图片)水印》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。