-
Notifications
You must be signed in to change notification settings - Fork 0
/
watermark.java
65 lines (51 loc) · 2 KB
/
watermark.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
/**
* Watermark in text
* requires itextpdf-5.5.4.jar
* @author jaypark@gmail.com 2014.12.24 (yes it's christmas)
*/
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.ColumnText;
public class watermark {
// for korean
static String FONTFILE = "./NanumGothic.ttf"; // korean font
static String LIC = "이 문서는 %s님께 사용이 허가된 문서입니다.";
// for english
//static String FONTFILE = "./times.ttf";
//static String LIC = "This document is licensed to %s.";
static BaseColor TCOLOR = new BaseColor(130, 130, 130);
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader(args[0]);
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(args[1]));
stamp.getWriter().setCompressionLevel(9); // without this code it'll 3 times bigger
int bottom_margin = Integer.parseInt(args[2]);
String lic = String.format(LIC, args[3]);
BaseFont bf = BaseFont.createFont(FONTFILE, BaseFont.IDENTITY_H , BaseFont.EMBEDDED);
Chunk c = new Chunk(lic, new Font(bf, 7, Font.NORMAL, TCOLOR));
c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1/800f, null);
Phrase phrase = new Phrase(c);
int pages = reader.getNumberOfPages();
int xpos = 120;
for (int i = 1; i <= pages; i++) {
if(i %2==0)
xpos = 160;
PdfContentByte under = stamp.getUnderContent(i);
ColumnText.showTextAligned(under, Element.ALIGN_LEFT, phrase, xpos, bottom_margin, -0);
}
stamp.setFullCompression(); // without this code it'll 3 times bigger
stamp.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}