import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter; public class PdfFileExportUtil { private static Font pdf8Font = null; private static Font pdf20Font = null; /**
*
* 设置PDF创建者信息
*
* @param pdfDocument
*/ public static Document setCreatorInfo(Document pdfDocument) { if (pdfDocument == null) { return null; } // 文档属性
pdfDocument.addTitle("北京XX科技有限公司综合分析PDF文档");
pdfDocument.addAuthor("北京XX科技有限公司");
pdfDocument.addSubject("XX分析页");
pdfDocument.addKeywords("综合分析");// 文档关键字信息
pdfDocument.addCreator("XX数据平台");// 应用程序名称
return pdfDocument;
} /**
*
* 获取中文字符集且是8号字体,常用作表格内容的字体格式
*
* @param fullFilePath
*/ public static Font getChinese8Font() throws DocumentException, IOException { if (pdf8Font == null) { // 设置中文字体和字体样式
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
pdf8Font = new Font(bfChinese, 8, Font.NORMAL); } return pdf8Font;
} /**
*
* 获取中文字符集且是8号字体,常用作文字水印信息
*
* @param fullFilePath
*/ public static Font getChinese20Font() throws DocumentException, IOException { if (pdf20Font == null) { // 设置中文字体和字体样式
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
pdf20Font = new Font(bfChinese);
pdf20Font.setSize(5);
} return pdf20Font;
} /**
*
* 设置成只读权限
*
* @param pdfWriter
*/ public static PdfWriter setReadOnlyPDFFile(PdfWriter pdfWriter)
throws DocumentException { pdfWriter.setEncryption(null, null, PdfWriter.ALLOW_PRINTING,
PdfWriter.STANDARD_ENCRYPTION_128); return pdfWriter;
} /**
*
* 变更一个图片对象的展示位置和角度信息
*
* @param waterMarkImage
*
* @param xPosition
*
* @param yPosition
*
* @return
*/ public static Image getWaterMarkImage(Image waterMarkImage,
float xPosition, float yPosition) { waterMarkImage.setAbsolutePosition(xPosition, yPosition);// 坐标 waterMarkImage.setRotation(-20);// 旋转 弧度 waterMarkImage.setRotationDegrees(-45);// 旋转 角度 waterMarkImage.scalePercent(100);// 依照比例缩放
return waterMarkImage;
} /**
*
* 为PDF分页时创建添加文本水印的事件信息
*/ public class TextWaterMarkPdfPageEvent extends PdfPageEventHelper { private String waterMarkText; public TextWaterMarkPdfPageEvent(String waterMarkText) { this.waterMarkText = waterMarkText; } public void onEndPage(PdfWriter writer, Document document) { try { float pageWidth = document.right() + document.left();// 获取pdf内容正文页面宽度 float pageHeight = document.top() + document.bottom();// 获取pdf内容正文页面高度 // 设置水印字体格式 Font waterMarkFont = PdfFileExportUtil.getChinese20Font(); PdfContentByte waterMarkPdfContent = writer.getDirectContent();//决定了水印图层高低
Phrase phrase = new Phrase(waterMarkText, waterMarkFont);
for(int i = 0 ; i < 100; i ++){
for(int j = 0 ; j < 20 ; j ++ ){
ColumnText.showTextAligned(waterMarkPdfContent,Element.ALIGN_CENTER, phrase,pageWidth * 0.2f * j, pageHeight * 0.08f * i, 45);
}
} } catch (DocumentException de) { de.printStackTrace(); System.err.println("pdf watermark font:" + de.getMessage()); } catch (IOException de) { de.printStackTrace(); System.err.println("pdf watermark font:" + de.getMessage()); } } } /**
*
* 为PDF分页时创建添加图片水印的事件信息
*/ public class PictureWaterMarkPdfPageEvent extends PdfPageEventHelper { private String waterMarkFullFilePath; private Image waterMarkImage; public PictureWaterMarkPdfPageEvent(String waterMarkFullFilePath) { this.waterMarkFullFilePath = waterMarkFullFilePath; } public void onEndPage(PdfWriter writer, Document document) { try { float pageWidth = document.right() + document.left();// 获取pdf内容正文页面宽度 float pageHeight = document.top() + document.bottom();// 获取pdf内容正文页面高度 PdfContentByte waterMarkPdfContent = writer.getDirectContent(); // 仅设置一个图片实例对象,整个PDF文档只应用一个图片对象,极大减少因为增加图片水印导致PDF文档大小增加 if (waterMarkImage == null) { waterMarkImage = Image.getInstance(waterMarkFullFilePath); }
// 添加水印图片,文档正文内容采用横向三列,竖向两列模式增加图片水印
for(int i = 0 ; i < 100; i ++){
for(int j = 0 ; j < 4 ; j ++ ){
waterMarkPdfContent.addImage(getWaterMarkImage(waterMarkImage,pageWidth * 0.3f * j , pageHeight * 0.05f * i));
}
} PdfGState gs = new PdfGState();
gs.setFillOpacity(0.8f);// 设置透明度为0.2
gs.setStrokeOpacity(0.8f);
gs.setOverPrintStroking(true);
waterMarkPdfContent.setGState(gs); } catch (DocumentException de) { de.printStackTrace(); System.err.println("pdf watermark font:" + de.getMessage()); } catch (IOException de) { de.printStackTrace(); System.err.println("pdf watermark font:" + de.getMessage()); } } } /**
*
* 为PDF分页时创建添加header和footer信息的事件信息
*/ class HeadFootInfoPdfPageEvent extends PdfPageEventHelper { public HeadFootInfoPdfPageEvent() { } public void onEndPage(PdfWriter writer, Document document) { try { PdfContentByte headAndFootPdfContent = writer
.getDirectContent(); headAndFootPdfContent.saveState(); headAndFootPdfContent.beginText(); BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); headAndFootPdfContent.setFontAndSize(bfChinese, 10); // 文档页头信息设置 float x = document.top(-20); // 页头信息左面 headAndFootPdfContent.showTextAligned(
PdfContentByte.ALIGN_LEFT, "综合分析", document.left(), x, 0); // 页头信息中间 headAndFootPdfContent.showTextAligned(
PdfContentByte.ALIGN_CENTER, "第" + writer.getPageNumber() + "页", (document.right() + document.left()) / 2, x, 0); // 页头信息右面 headAndFootPdfContent.showTextAligned(
PdfContentByte.ALIGN_RIGHT, "北京XX科技有限公司", document.right(), x, 0); // 文档页脚信息设置 float y = document.bottom(-20); // 页脚信息左面 headAndFootPdfContent.showTextAligned(
PdfContentByte.ALIGN_LEFT, "--", document.left(), y, 0); // 页脚信息中间 headAndFootPdfContent.showTextAligned(
PdfContentByte.ALIGN_CENTER, "-", (document.right() + document.left()) / 2, y, 0); // 页脚信息右面 headAndFootPdfContent.showTextAligned(
PdfContentByte.ALIGN_RIGHT, "--", document.right(), y, 0); headAndFootPdfContent.endText(); headAndFootPdfContent.restoreState(); } catch (DocumentException de) { de.printStackTrace(); System.err.println("pdf watermark font:" + de.getMessage()); } catch (IOException de) { de.printStackTrace(); System.err.println("pdf watermark font:" + de.getMessage()); } } }
}

使用方法:

            PdfFileExportUtil pdfFileExportUtil = new PdfFileExportUtil();
pdfWriter.setPageEvent(pdfFileExportUtil.new PictureWaterMarkPdfPageEvent(basePath+"/images/xxx.png"));

itext之pdf导出添加水印Java工具类的更多相关文章

  1. 使用Apache poi来编写导出excel的工具类

    在JavaWeb开发的需求中,我们会经常看到导出excel的功能需求,然后java并没有提供操作office文档的功能,这个时候我们就需要使用额外的组件来帮助我们完成这项功能了. 很高兴Apache基 ...

  2. java工具类系列 (四.SerializationUtils)

    java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...

  3. Java工具类——通过配置XML验证Map

    Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...

  4. 排名前 16 的 Java 工具类

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  5. 排名前16的Java工具类

    原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...

  6. 第一章 Java工具类目录

    在这一系列博客中,主要是记录在实际开发中会常用的一些Java工具类,方便后续开发中使用. 以下的目录会随着后边具体工具类的添加而改变. 浮点数精确计算 第二章 Java浮点数精确计算 crc32将任意 ...

  7. java工具类之按对象中某属性排序

    import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...

  8. 干货:排名前16的Java工具类

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  9. Java工具类:给程序增加版权信息

       我们九天鸟的p2p网贷系统,基本算是开发完成了.   现在,想给后端的Java代码,增加版权信息.   手动去copy-paste,太没有技术含量. 于是,写了个Java工具类,给Java源文件 ...

随机推荐

  1. Script:SQL调优健康检查脚本

    Script:SQL调优健康检查脚本 http://www.askmaclean.com/archives/sql-tuning-health-check-script.html 以下脚本可以用于收集 ...

  2. 根据自己的需要,把别人开发好的东西搬过来,优化and重构,在优化的过程中,甚至也会弄出一套全新的东西(转)

    赵海平在今年三月份来到阿里,听毕玄(他现任主管)说去年五六月份就跟赵海平聊上了.有人问:为啥 BAT 三大巨头,你看中了阿里巴巴?在今天现场达一千多人的分享中赵海平给出了回复:“因为百度和腾讯没找我呗 ...

  3. 024找到二维阵列(keep it up)

    剑指offer在标题中:http://ac.jobdu.com/problem.php? pid=1384 题目描写叙述: 在一个二维数组中,每一行都依照从左到右递增的顺序排序.每一列都依照从上到下递 ...

  4. AndroidUI组件之TabHost

    package com.gc.tabhost; /** * @author Android将军 * * * * 1.TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置 * 多 ...

  5. 安装、设置与启动MySql绿色版的方法

    原文:安装.设置与启动MySql绿色版的方法 1.解压 mysql-noinstall-5.1.30-win32.zip(下载地址http://dev.mysql.com/downloads/mysq ...

  6. NSIS API 函数常用备份

    原文:NSIS API 函数常用备份 关闭程序: System::Call `user32::AnimateWindow(i$HWNDPARENT,i200,i${AW_BLEND}|${AW_HID ...

  7. SQLServer-----SQLServer 2008 R2卸载

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVrZXdhbmd6aQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  8. hdu4699 Editor 2013 多校训练第十场 D题 数列维护 splay | 线段树 | 栈!!!!!

    题意:维护一个文本编辑,并且查询最大前缀和. 写了splay,wa了13次 过了之后觉着特傻逼.发现题解两个栈就可以了,光标前后维护两个栈,维护前面的栈的前缀和 和 最大前缀和. 哎,傻逼,太弱了,还 ...

  9. linux 核心学习书籍

    1<LINUX程序设计> 基础是非常重要的 2<UNIX环境高级编程> 还是基础 ,假设你是搞UNIX/Linux环境下的应用程序编程,那么就看 3, <Linux内核设 ...

  10. 4、Cocos2dx 3.0三,找一个小游戏开发Hello World 分析

    尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27186557 Hello World 分析 打开新 ...