import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class PDFWatermark {
/**
*
*
* 添加文字水印
* @param pdfPath
* PDF路径
* @@param list
* 一个map集合,map固定的四个key作为参数,value:要输入的值,x:x轴,y:y轴,angle:旋转角度
* Map<String,String> map=new HashMap<String,String>();
* map.put("value","测试水印");
* map.put("x","70");
* map.put("y","350");
* map.put("angle","0");
* @throws IOException
*/
public static Map<String,String> setWatermark(String pdfPath,List<Map<String,String>> list){
Map<String,String> result=new HashMap<String,String>();
PdfReader reader=null;
BufferedOutputStream bos=null;
PdfStamper stamper=null;
try{
reader = new PdfReader(pdfPath);
int index=pdfPath.lastIndexOf(".");
String newPath=pdfPath.substring(0,index)+"_01.pdf";
bos = new BufferedOutputStream(new FileOutputStream(new File(newPath)));
//bos = new BufferedOutputStream(new FileOutputStream(new File("C:/Users/cgh/Desktop/20181107_pdf水印/文字.pdf")));
stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
//BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
//BaseFont base = BaseFont.createFont("C:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//拿到字体包
BaseFont base = BaseFont.createFont(DownloadAttachments.dirPath+File.separator+"SIMSUN.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//BaseFont base=BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.WINANSI,BaseFont.EMBEDDED);
for (int i = 1; i < total; i++) {
content = stamper.getOverContent(i);// 在内容上方加水印
// content = stamper.getUnderContent(i);//在内容下方加水印
content.beginText();
content.setColorFill(Color.red);
content.setFontAndSize(base,14);
for(Map<String, String> map:list){
String value=""; //需要水印的内容
int x=0; //x轴
int y=0; //y轴
int angle=0; //旋转角度
for (Map.Entry<String, String> entry : map.entrySet()) {
if(entry.getKey().equals("value")){
value=entry.getValue();
}else if(entry.getKey().equals("x")){
x=Integer.valueOf(entry.getValue());
}else if(entry.getKey().equals("y")){
y=Integer.valueOf(entry.getValue());
}else if(entry.getKey().equals("angle")){
angle=Integer.valueOf(entry.getValue());
}
}
content.showTextAligned(Element.ALIGN_CENTER , value, x, y, angle);
}
content.endText();
}
result.put("flag", "SUCCESS");
result.put("newPath", newPath);
return result;
}catch(Exception e){
e.printStackTrace();
result.put("flag", "FAIL");
result.put("msg", e.toString());
return result;
}finally{
if(stamper!=null){
try {
stamper.close();
stamper=null;
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bos!=null){
try {
bos.close();
bos=null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(reader!=null){
reader.close();
reader=null;
}
}
}
public static void main(String[] args){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String text=format.format(new Date());
List<Map<String,String>> list=new ArrayList<Map<String,String>>();
Map<String,String> map=new HashMap<String,String>();
map.put("value",text);
map.put("x","70");
map.put("y","350");
map.put("angle","0");
Map<String,String> map1=new HashMap<String,String>();
map1.put("value","206所受控");
map1.put("x","70");
map1.put("y","330");
map1.put("angle","0");
list.add(map);
list.add(map1);
setWatermark("C:/Users/cgh/Desktop/20181107_pdf水印/文字内容设计文件模板.pdf",list);
}
}