Java使用itexpdf创建pdf文件并添加水印

1. 添加依赖

1
2
3
4
5
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>

2. java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;
import java.util.Random;


/**
* @author pibigstar
* @create 2018-11-30 13:30
* @desc pdf工具类
**/
public class PDFUtil {
private static final float IAMGE_HEIGHT = 110f; // 水印图片的的高度
private static final float IAMGE_WIDTH = 110f; // 水印图片的的宽度

/**
* @Description: 创建pdf文件
* @param filePath: 输出文件路径
* @param contents:文件内容
*/
public static void createPdf(String filePath,String contents) {
Document document = null;
try {
document = new Document(PageSize.A4);
PdfWriter.getInstance(document,new FileOutputStream(filePath));
document.open();
//添加创建时间
document.addCreationDate();

//使用Windows系统字体(解决中文显示问题)
BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont);
// 设置一段内容
Paragraph paragraph = new Paragraph(contents,font);
//设置居中对齐
paragraph.setAlignment(Element.ALIGN_CENTER);

document.add(paragraph);

}catch (Exception e){
e.printStackTrace();
}finally {
if (document!=null){
document.close();
}
}
}

/**
* @Description: 添加Pdf水印
* @param pdfPath pdf地址
* @param logoPath 水印图片地址
* @param outPath 输出pdf地址
*/
public static void addPdfMark(String pdfPath,String logoPath, String outPath) throws Exception {

PdfReader reader = new PdfReader(pdfPath, "PDF".getBytes());
// 如果是web项目,直接下载应该放到response的流里面
// PdfStamper stamp = new PdfStamper(reader,response.getOutputStream());
// 添加水印之后的pdf文件
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outPath));
int pageSize = reader.getNumberOfPages();

float pageHeight = reader.getPageSize(1).getHeight();
float pageWidth = reader.getPageSize(1).getWidth();
try {
// 每两行显示两个 左边一个,右边一个
int lineNum = (int) (pageHeight / IAMGE_HEIGHT); // 行数
int middleY = (int) pageWidth / 2;
for (int i = 1; i <= pageSize; i++) {
for (int j = 0, k = 0; j < lineNum; j = j + 4, k++) {
Random random = new Random();
// 放入水印
Image img = Image.getInstance(logoPath);
img.scaleAbsolute(IAMGE_WIDTH, IAMGE_HEIGHT * 184 / 455);
// 将水印显示到最底层
img.setAlignment(Image.UNDERLYING);
int trueY;
while (true) {
trueY = random.nextInt(middleY);
if (trueY > IAMGE_WIDTH / 2 && trueY < (middleY - IAMGE_WIDTH)) {
break;
}
}
// 水印的位置
img.setAbsolutePosition(trueY, j * IAMGE_HEIGHT + (float) random.nextInt((int) IAMGE_HEIGHT) - (k % 2) * 10);
// 旋转 角度
img.setRotationDegrees(random.nextInt(360));
PdfContentByte under = stamp.getUnderContent(i);

PdfGState gs = new PdfGState();
// 设置透明度为0.5
gs.setFillOpacity(0.5f);
under.setGState(gs);
under.addImage(img);
while (true) {
trueY = random.nextInt(middleY) + middleY;
if (trueY > middleY + IAMGE_WIDTH / 2 && trueY < (2 * middleY - IAMGE_WIDTH)) {
break;
}
}
// 水印的位置
img.setAbsolutePosition(trueY, j * IAMGE_HEIGHT + (float) random.nextInt((int) IAMGE_HEIGHT) - (k % 2) * 10);
// 旋转角度
img.setRotationDegrees(random.nextInt(360));
under.addImage(img);
}
}
} catch (Exception e) {
throw e;
} finally {
stamp.close();
reader.close();
}

}
}

3. 测试

1
2
3
4
5
6
7
public class TestPDFUtil {
public static void main(String[] args) throws Exception {
PDFUtil.createPdf("D:\\1.pdf","我是派大星");
PDFUtil.addPdfMark("D:\\1.pdf","D:\\go_logo.png","D:\\test.pdf");
System.out.println("done");
}
}
-------------本文结束感谢您的阅读-------------