itext之pdf导出添加水印Java工具类
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工具类的更多相关文章
- 使用Apache poi来编写导出excel的工具类
在JavaWeb开发的需求中,我们会经常看到导出excel的功能需求,然后java并没有提供操作office文档的功能,这个时候我们就需要使用额外的组件来帮助我们完成这项功能了. 很高兴Apache基 ...
- java工具类系列 (四.SerializationUtils)
java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
- 排名前 16 的 Java 工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- 排名前16的Java工具类
原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...
- 第一章 Java工具类目录
在这一系列博客中,主要是记录在实际开发中会常用的一些Java工具类,方便后续开发中使用. 以下的目录会随着后边具体工具类的添加而改变. 浮点数精确计算 第二章 Java浮点数精确计算 crc32将任意 ...
- java工具类之按对象中某属性排序
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...
- 干货:排名前16的Java工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- Java工具类:给程序增加版权信息
我们九天鸟的p2p网贷系统,基本算是开发完成了. 现在,想给后端的Java代码,增加版权信息. 手动去copy-paste,太没有技术含量. 于是,写了个Java工具类,给Java源文件 ...
随机推荐
- Flex入门(三)——微架构之Cairngorm
大家都知道我们在开发后台的时候,都会使用MVC,三层等分层架构,使后台代码达到职责更为分明单一,高内聚低耦合,比如,Dao层仅仅是进行和数据库打交道,负责处理数据:Service(B层)仅仅是进行逻辑 ...
- myeclipse 8.5-10.0 安装 svn 方法(转)
方法总结 方法一:在线安装 1.打开HELP->MyEclipse Configuration Center.切换到SoftWare标签页. 2.点击Add Site 打开对话框 ...
- 使用SQLCMD在SQLServer执行多个脚本
原文:使用SQLCMD在SQLServer执行多个脚本 概述: 作为DBA,经常要用开发人员提供的SQL脚本来更新正式数据库,但是一个比较合理的开发流程,当提交脚本给DBA执行的时候,可能已经有几百个 ...
- 创建在SQLServer 和 Oracle的 DBLINK
dblink 当我们要跨本地数据库.訪问另外一个数据库表中的数据时,本地数据库中就必需要创建远程数据库的dblink,通过dblink本地数据库能够像訪问本地数据库一样訪问远程数据库表中的数据. 一 ...
- hdoj1010Starship Troopers (树dp,依赖背包)
称号:hdoj1010Starship Troopers 题意:有一个军队n个人要占据m个城市,每一个城市有cap的驻扎兵力和val的珠宝,并且这m个城市的占率先后具有依赖关系,军队的每一个人能够打败 ...
- myeclipse中间classpath
myeclipse中间classpath这是一个非常重要的问题 myeclipse是搜索寻找在按照时间其,和myeclipse有一个特殊的文件来保存classpath信息.这也是别人的项目文件的副本时 ...
- zoj-3795-Grouping-tarjan确定最长的公路收缩
使用tarjan缩合点. 然后,dfs寻找最长的公路. 水体. . . #include<stdio.h> #include<string.h> #include<alg ...
- DP Leetcode - Maximum Product Subarray
近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了..思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]> ...
- HDU4893:Wow! Such Sequence!(段树lazy)
Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...
- HR系统邮件审批功能总结
时至今日,来兰亭工作的第三个小任务算是暂时告一段落了.这个小任务是耗费时间最长的,因此在这里总结整理一下. 首先这个功能的出发点是方法领导进行手下员工的审批,包括加班申请,休假申请,和漏打卡申请.由于 ...