java批量给pdf加水印

找了一圈pdf在mac下的水印工具,都不太好用。java写一个吧

以下代码依赖itext5.3.3包,

从我的网盘下载

java操作pdf 依靠itext5.3.3

//读取原来的pdf
PdfReader reader = new PdfReader("/test/" + "1.pdf");
//生成以后的pdf
		PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("/test/" + "createSamplePDF2.pdf"));
		int max = reader.getNumberOfPages();
		//max =2;
		// 文字水印
		for (int i = 1; i <= max; i++) {
			PdfContentByte over = stamp.getOverContent(i);
			over.beginText();
			BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
			over.setFontAndSize(bf, 16);
			over.setTextMatrix(10, 10);
			over.setColorFill(BaseColor.GRAY);
			over.showTextAligned(Element.ALIGN_LEFT, "java blog java-er.com ", 150,670, 0);
// 0 0 0 表示左下脚 最后一个0是角度,0表示横着 45 表示斜着
			over.endText();
		}
 
		reader.close();
		stamp.close();
 
	}


This entry was posted in JAVA and tagged , , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/java-pdf-walter-mark/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

  

  

About Me

静水流深,水滴石穿