iText PDF使用示例

目前生成PDF主要是使用itext pdf,最近工作原因重新使用了下,目前大部分也只是集中在生成段落文字,支持中文,生成水印,加盖印章.网络上大部分的例子对于水印都是对已经存在的PDF进行操作,这边介绍使用PageEvent一个一边生成文档内容一边生成水印的办法.

首先,要是用的话依赖于itext包,如果有中文字体的话增加asian依赖,其中也可以使用自定义字体.

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

代码示例如下

public static void main(String[] args)throws Exception {
		
	File file = new File("itext.pdf");
	if(!file.exists())
		file.createNewFile();
	
	OutputStream os = new FileOutputStream(file);
	Document document = new Document();
	PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
	
	//打开文档
	document.open();
	
	//每页使用
	pdfWriter.setPageEvent(new WatermarkPageEvent("this is the watermark"));
	
	//设置字体
	BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	Font font = new Font(bfChinese);	
	
	//创建段落
	Paragraph paragraph = null;
	
	for(int i=0;i<50;i++){
		paragraph = new Paragraph("this is a paragraph,this is a paragraph,this is a paragraph,this is a paragraph,this is a paragraph",font);
		document.add(paragraph);
	}
	
	//读入并设置印章图片
	Image image = Image.getInstance("seal.png");
	image.setScaleToFitLineWhenOverflow(true);
	image.setAbsolutePosition(100, 670);
	image.scaleAbsolute(125, 125);
	
	//写入印章
	PdfContentByte pcb = pdfWriter.getDirectContent();
	pcb.addImage(image);
	
	//关闭文档
	document.close();
}

注释已经很详细了,生成水印的代码主要在WatermarkPageEvent类里面

package com.github.lihongkun.itext;

import org.apache.commons.lang3.StringUtils;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 水印事件
 * 
 * @author lihongkun
 */
public class WatermarkPageEvent extends PdfPageEventHelper {

	private Font font;

	private String text;

	public WatermarkPageEvent(String text) {
		this(text, null);
	}

	public WatermarkPageEvent(String text, Font font) {
		this.text = text;
		if (font == null) {
			try {
				BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
				this.font = new Font(bfChinese, 36, Font.BOLD, new GrayColor(0.90f));
			} catch (Exception ex) {
				throw new RuntimeException(ex);
			}
		} else
			this.font = font;
	}

	/**
	 * 每页事件
	 */
	public void onEndPage(PdfWriter writer, Document document) {
		ColumnText.showTextAligned(writer.getDirectContentUnder(), Element.ALIGN_LEFT,
				new Phrase(StringUtils.isEmpty(text) ? "" : this.text, font), 85, 550, 25);
		
		ColumnText.showTextAligned(writer.getDirectContentUnder(), Element.ALIGN_LEFT,
				new Phrase(StringUtils.isEmpty(text) ? "" : this.text, font), 85, 350, 25);
		
		ColumnText.showTextAligned(writer.getDirectContentUnder(), Element.ALIGN_LEFT,
				new Phrase(StringUtils.isEmpty(text) ? "" : this.text, font), 85, 150, 25);

	}

}

完整代码放于https://github.com/lihongkun/itext上面,可自行执行效果.对于不同的水印文字和印章需要微调下位置.



留个爪印