lusiqi

所需jar包

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>

不要引com.lowagie的jar包,在实现过程中com.lowagie会导致字体无法引入,报错:
Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.
更换com.itextpdf的itext后解决此问题。

代码

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
/**
*
* 【功能描述:添加图片和文字水印】 【功能详细描述:功能详细描述】
* @param srcFile 待加水印文件
* @param destFile 加水印后存放地址
* @param text 加水印的文本内容
* @throws Exception
*/
public void setWaterMark(String input, String output, String text,
int textWidth, int textHeight) throws Exception
{
// 待加水印的文件
PdfReader reader = new PdfReader(input);
// 加完水印的文件
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
output));
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
// 设置字体
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
// 循环对每页插入水印
for (int i = 1; i < total; i++)
{
// 水印的起始 下面
content = stamper.getUnderContent(i);
//上面覆盖水印
//content = stamper.getOverContent(i);
//透明度
PdfGState gs = new PdfGState();
gs.setFillOpacity(2f);
gs.setStrokeOpacity(2f);
content.setGState(gs);

// 开始
content.beginText();
// 设置颜色
//content.setColorFill(BaseColor.BLACK);
content.setColorFill(Color.GRAY);
// 设置字体及字号
content.setFontAndSize(font, 50);
// 设置起始位置
//content.setTextMatrix(70, 200);
content.setTextMatrix(textWidth, textHeight);
// 开始写入水印
content.showTextAligned(Element.ALIGN_LEFT, text, 300,350, 45);

//图片
Image image = Image.getInstance("G:/2.jpeg");
img.setAlignment(Image.LEFT | Image.TEXTWRAP);
img.setBorder(Image.BOX); img.setBorderWidth(10);
img.setBorderColor(BaseColor.WHITE); img.scaleToFit(100072);//大小
img.setRotationDegrees(-30);//旋转
image.setAbsolutePosition(200, 206); // set the first background
// image of the absolute
image.scaleToFit(200, 200);
content.addImage(image);
content.setColorFill(Color.BLACK);
content.setFontAndSize(base, 8);
content.showTextAligned(Element.ALIGN_CENTER, text, 300, 10, 0);

content.endText();
}
stamper.close();
bos.close();
reader.close();
}

 评论