1.概述 添加文档的水印可以通过继承PdfPageEventHelper来完成。
代码如下
- import com.itextpdf.text.Document;
-
import com.itextpdf.text.Element;
-
import com.itextpdf.text.Font;
-
import com.itextpdf.text.Font.FontFamily;
-
import com.itextpdf.text.Phrase;
-
import com.itextpdf.text.pdf.ColumnText;
-
import com.itextpdf.text.pdf.GrayColor;
-
import com.itextpdf.text.pdf.PdfPageEventHelper;
-
import com.itextpdf.text.pdf.PdfWriter;
-
-
/**
-
* 水印类.
-
*
-
* @version V1.0 ,2012-1-1
-
* @author xiahui
-
*
-
*/
-
public class Watermark extends PdfPageEventHelper{
-
Font font=new Font(FontFamily.HELVETICA,52,Font.BOLD,new GrayColor(0.75f));
-
-
public void onEndPage (PdfWriter writer, Document document) {
-
ColumnText.showTextAligned(
-
writer.getDirectContentUnder(),
-
Element.ALIGN_CENTER,
-
new Phrase(" watermovie",font),
-
297.5f,
-
421,
-
writer.getPageNumber()%2==1?45:-45
-
);
-
}
-
}
运行代码参见HeaderAndFooterDemo
参考文献1.Adding page events to PdfWriter (iText 5).
阅读(2480) | 评论(0) | 转发(0) |