Java itext 为 pdf文件添加水印

需要Java包

<dependency>
		<groupId>com.itextpdf</groupId>
		<artifactId>itextpdf</artifactId>
		<version>5.5.9</version>
</dependency>
<dependency>
		<groupId>com.itextpdf</groupId>
		<artifactId>itext-asian</artifactId>
		<version>5.2.0</version>
</dependency>

java代码

/**
	 * 中间或者两边水印
	 * @param bos   添加完水印的输出
	 * @param input 原PDF文件输入
	 * @param word  水印内容
	 * @param model 水印添加位置1中间,2两边
	 */
	public static setWatermark(BufferedOutputStream bos, InputStream input, String word, int model)
			throws DocumentException, IOException {
		PdfReader reader = new PdfReader(input);
		PdfStamper stamper = new PdfStamper(reader, bos);
		PdfContentByte content;
		// 创建字体,第一个参数是字体路径,itext有一些默认的字体比如说:
		//BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
		BaseFont base = BaseFont.createFont("/msyh.ttf", BaseFont.IDENTITY_H,
				BaseFont.EMBEDDED);
		PdfGState gs = new PdfGState();
		// 获取PDF页数
		int total = reader.getNumberOfPages();
		// 遍历每一页
		for (int i = 0; i < total; i++) {
			float width = reader.getPageSize(i + 1).getWidth(); // 页宽度
			float height = reader.getPageSize(i + 1).getHeight(); // 页高度
			content = stamper.getOverContent(i + 1);// 内容
			content.beginText();//开始写入文本  
			gs.setFillOpacity(0.3f);//水印透明度
			content.setGState(gs);
			content.setColorFill(BaseColor.LIGHT_GRAY);
			content.setTextMatrix(70, 200);//设置字体的输出位置  

			if (model == 1) { //平行居中的3条水印
				content.setFontAndSize(base, 50); //字体大小
				//showTextAligned 方法的参数分别是(文字对齐方式,位置内容,输出水印X轴位置,Y轴位置,旋转角度)
				content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 650, 30);
				content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 400, 30);
				content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 150, 30);
			} else { // 左右两边个从上到下4条水印
				float rotation = 30;// 水印旋转度数
			
				content.setFontAndSize(base, 20);
				content.showTextAligned(Element.ALIGN_LEFT, word, 20, height - 50, rotation);
				content.showTextAligned(Element.ALIGN_LEFT, word, 20, height / 4 * 3 - 50, rotation);
				content.showTextAligned(Element.ALIGN_LEFT, word, 20, height / 2 - 50, rotation);
				content.showTextAligned(Element.ALIGN_LEFT, word, 20, height / 4 - 50, rotation);

				content.setFontAndSize(base, 22);
				content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height - 50, rotation);
				content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height / 4 * 3 - 50, rotation);
				content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height / 2 - 50, rotation);
				content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height / 4 - 50, rotation);
			}
			content.endText();//结束写入文本  
			//要打图片水印的话
			//Image image = Image.getInstance("c:/1.jpg");  
			//content.addImage(image);  
		}
		stamper.close();
	}

使用例子:

public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {
		setWatermark(new BufferedOutputStream(new FileOutputStream(new File("/Users/mimhope/Documents/abc.pdf"))),
				new FileInputStream("/Users/mimhope/Documents/周一.pdf"), "测试打印", 1);
	}

效果:
在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值