360SDN.COM

首页/Java/列表

java使用itext的showTextAligned方法给pdf添加文字水印(watermark)

来源:  2013-07-22 17:39:59    评论:0点击:

java的开源pdf库-Itext可以给pdf添加水印,主要是使用showTextAligned这个方法.ShowTextAligned方法可以设置输出水印的文本和水印文本的旋转角度,ShowTextAligned方法具体的参数说明如下:

public void ShowTextAligned(int alignment, String text, float x,float y, float rotation)

参数 参数说明:
alignment 左、右、居中(ALIGN_CENTER, ALIGN_RIGHT or ALIGN_LEFT)
text 要输出的文本
x 文本输入的X坐标
y 文本输入的Y坐标
rotation 文本的旋转角度

将itext-5.4.2.zip压缩包解压缩后得到7个文件:itextpdf-5.4.2.jar(核心组件)、itextpdf-5.4.2-javadoc.jar(API文档)、itextpdf-5.4.2-sources.jar(源代码)、itext-xtra-5.4.2.jar、itext-xtra-5.4.2-javadoc.jar、itext-xtra-5.4.2-sources.jar ,如果是输出中文pdf,需要用到itext-asian.jar这个jar包,这个jar包在extrajars-2.3.zip中,下载地址:请点这里

private static void addWatermark(PdfStamper pdfStamper
, String waterMarkName) {
PdfContentByte content = null;
BaseFont base = null;
Rectangle pageRect = null;
PdfGState gs = new PdfGState();
try { // 设置字体
base = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (base == null || pdfStamper == null) { return; }
// 设置透明度为0.4
gs.setFillOpacity(0.4f);
gs.setStrokeOpacity(0.4f);
int toPage = pdfStamper.getReader().getNumberOfPages();
for (int i = 1; i <= toPage; i++) {
pageRect = pdfStamper.getReader(). getPageSizeWithRotation(i);
// 计算水印X,Y坐标
float x = pageRect.getWidth() / 2;
float y = pageRect.getHeight() / 2;
//获得PDF最顶层
content = pdfStamper.getOverContent(i);
content.saveState();
// set Transparency
content.setGState(gs);
content.beginText();
content.setColorFill(BaseColor.GRAY);
content.setFontAndSize(base, 60);
// 水印文字成45度角倾斜
content.showTextAligned(Element.ALIGN_CENTER ,
waterMarkName, x, y, 45); content.endText(); }
} catch (Exception ex) {
ex.printStackTrace();
}
finally {
content = null;
base = null;
pageRect = null;
}
}

java使用itext的showTextAligned方法给pdf添加文字水印的完整代码如下:

package com.pdf;
 
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;

public class ItextHelloword {
 
	public static void main(String[] args) throws Exception {
		String pdfFilePath = "f:/itext-demo.pdf"; 
		PdfReader pdfReader = new PdfReader("f:/itext-demo.pdf");
		// Get the PdfStamper object
		PdfStamper pdfStamper = new PdfStamper(pdfReader
		    , new FileOutputStream(
				"f:/itext-demo22.pdf"));
		addWatermark(pdfStamper, "www.360sdn.com");

		pdfStamper.close(); 
	}

	 private static void addWatermark(PdfStamper pdfStamper
	      , String waterMarkName) {
			PdfContentByte content = null;
			BaseFont base = null;
			Rectangle pageRect = null;
			PdfGState gs = new PdfGState();
			try {
				// 设置字体
			base = BaseFont.createFont("STSongStd-Light", 
"UniGB-UCS2-H",
		BaseFont.NOT_EMBEDDED);
			} catch (DocumentException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (base == null || pdfStamper == null) {
					return;
				}
				// 设置透明度为0.4
				gs.setFillOpacity(0.4f);
				gs.setStrokeOpacity(0.4f);
				int toPage = pdfStamper.getReader().getNumberOfPages();
				for (int i = 1; i <= toPage; i++) {
					pageRect = pdfStamper.getReader().
					   getPageSizeWithRotation(i);
					// 计算水印X,Y坐标
					float x = pageRect.getWidth() / 2;
					float y = pageRect.getHeight() / 2;
					//获得PDF最顶层
					content = pdfStamper.getOverContent(i);
					content.saveState();
					// set Transparency
					content.setGState(gs);
					content.beginText();
					content.setColorFill(BaseColor.GRAY);
					content.setFontAndSize(base, 60);
					// 水印文字成45度角倾斜
					content.showTextAligned(Element.ALIGN_CENTER
							, waterMarkName, x,
							y, 45);
					content.endText(); 
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			} finally {
				content = null;
				base = null;
				pageRect = null;
			}
		}
 }		
java使用itext的showTextAligned方法给pdf添加文字水印(watermark)
为您推荐

友情链接 |九搜汽车网 |手机ok生活信息网|ok生活信息网|ok微生活
 Powered by www.360SDN.COM   京ICP备11022651号-4 © 2012-2016 版权