打印PDF水印

package com.pdf.print;

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

/**
 * 该类用来表示对PDF文档进行打水印操作
 * @author Administrator
 *
 */

public class Test {
	
	public static void main(String[] args) throws Exception {
		PdfReader pdfReader = new PdfReader("file:/E:/googleDownload/ibatis 开发指南.pdf");
		PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("E:/water.pdf"));;
		int total = pdfReader.getNumberOfPages() + 1;
		PdfContentByte content;
		// 设置字体
		BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
		// 水印文字
		String str = "My PDF";
		int j = str.length();
		char c = 0;
		int high = 0;
		for(int i=1;i<total;i++){
			high = 500;
			content = pdfStamper.getUnderContent(i);
			content.beginText(); // 开始
			content.setColorFill(Color.GRAY); // 设置颜色
			content.setFontAndSize(font, 18);
			content.setTextMatrix(400, 780);
			for(int k=0;k<j;k++){
				content.setTextRise(14);
				c = str.charAt(k);
				content.showText(c + "");
				high -= 5;
			}
			content.endText();
		}
		pdfStamper.close();
	}

}

所需jar包请参阅附件 

该文章引用地址:http://huangjinping.iteye.com/blog/1561595

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值