KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Stamp


1 // $Id: Stamp.java,v 1.5 2003/10/30 18:26:31 mike Exp $
2

3 import org.faceless.pdf2.*;
4 import java.io.*;
5 import java.awt.Color JavaDoc;
6 import java.util.List JavaDoc;
7
8 // An example which adds a "Confidential" rubber stamp to each page.
9
// Takes a filename as an argument, and creates "Stamp.pdf" as output.
10
//
11
// We demonstrate 4 different ways to do this, each with it's own pros and cons:
12
//
13
// Method 1: Adding a read-only annotation to the page
14
// Method 2: Adding some text "under" the page contents
15
// Method 3: Adding some outlined text over the page contents
16
// Method 4: Adding some translucent text over the page contents
17
//
18
// Each method can be tried just by uncommenting the appropriate calls to stampX
19
// and initstampX
20
//
21
//
22
public class Stamp
23 {
24     static PDFStyle stampstyle;
25
26     public static void main(String JavaDoc[] args) throws IOException
27     {
28     if (args.length==0) {
29         System.err.println("Usage: java Stamp <filename.pdf>");
30         System.err.println(" Creates the file \"Stamp.pdf\"");
31         System.exit(0);
32     }
33
34     // Initialize stampstyle (depending on which type of stamp is used)
35
//
36
initstamp1();
37 // initstamp2();
38
// initstamp3();
39
// initstamp4();
40

41
42     // Load the PDF
43
InputStream in = new FileInputStream(args[0]);
44     PDF pdf = new PDF(new PDFReader(in));
45     in.close();
46
47     // For each page...
48
List JavaDoc pages = pdf.getPages();
49     for (int i=0;i<pages.size();i++) {
50         PDFPage page = (PDFPage)pages.get(i);
51
52         float midx = page.getWidth()/2; // Position stamp just up
53
float midy = page.getHeight()/1.3f; // from the page center
54

55         // Add the stamp. Uncomment one of these and the matching
56
// initstamp() call above.
57
//
58
stamp1(page, midx, midy);
59 // stamp2(page, midx, midy);
60
// stamp3(page, midx, midy);
61
// stamp4(page, midx, midy);
62
}
63
64     pdf.render(new FileOutputStream("Stamp.pdf"));
65     }
66
67     //------------------------------------------------------------------------
68
// The first type of stamp - adds a translucent annotation to the page and
69
// set it to read-only, preventing it from being moved or deleted.
70
//
71
// Pros: * A number of predefined stamps to choose from, or create your
72
// own without much difficulty
73
// * Guaranteed to be visible - appears above any other annotations
74
// except form fields.
75
// * Stamp can optionally be displayed only when the document is
76
// printed (or only when it's on screen). This is only possible with
77
// annotations.
78
// * If all you're doing is reading, stamping and writing a document,
79
// then each page won't have to be decompressed/recompressed. Which
80
// makes this method potentially the fastest.
81
//
82
// Cons: * Translucency works only in Acrobat 5 or later.
83
// * With a knowledge of the PDF spec, the stamp could be removed by
84
// someone with a PDF API (like this one) or a hex editor. Certainly
85
// not something an average user could do.
86
//
87
static void initstamp1()
88     {
89     }
90
91     static void stamp1(PDFPage page, float midx, float midy)
92     {
93     AnnotationStamp stamp = new AnnotationStamp("stamp.stencil.Confidential", 0.35f);
94     stamp.setReadOnly(true);
95     float w = stamp.getRecommendedWidth();
96     float h = stamp.getRecommendedHeight();
97     stamp.setRectangle(midx-(w/2), midy-(h/2), midx+(w/2), midy+(h/2));
98     page.getAnnotations().add(stamp);
99     }
100
101
102     //------------------------------------------------------------------------
103
// The second type of stamp - adds some regular text to the page "under"
104
// the current page content.
105
//
106
// Pros: * Difficult to remove even with an API and a knowledge of PDF.
107
// * Works in Acrobat 4 and earlier
108
// * Can use any text or image you like, in any layout.
109
//
110
// Cons: * Will be overwritten by page content, so solid blocks (images,
111
// filled rectangles etc.) will obscure it.
112
//
113
static void initstamp2()
114     {
115     stampstyle = new PDFStyle();
116     stampstyle.setFont(new StandardFont(StandardFont.COURIERBOLD), 60);
117     stampstyle.setTextAlign(PDFStyle.TEXTALIGN_CENTER);
118     stampstyle.setFillColor(new Color JavaDoc(255, 210, 210));
119     }
120
121     static void stamp2(PDFPage page, float midx, float midy)
122     {
123         page.seekStart(); // Move to start of page content
124
page.setStyle(stampstyle);
125     page.rotate(midx, midy, -20);
126     page.drawText("Confidential", midx, midy);
127     }
128
129
130     //------------------------------------------------------------------------
131
// The third type of stamp - again, adds text to the page but this time
132
// above the page body. We avoid obscuring the current content of the
133
// page by making the text outlined (the same way we do the "DEMO" stamp
134
// in the trial version).
135
//
136
// Pros: * Difficult to remove even with an API and a knowledge of PDF.
137
// * Works in Acrobat 4 and earlier.
138
// * Can use any text you like, in any layout or font.
139
//
140
// Cons: * The text has to be outlined.
141
// * Can be obscured by an annotation displayed above it.
142
//
143
static void initstamp3()
144     {
145     stampstyle = new PDFStyle();
146     stampstyle.setFont(new StandardFont(StandardFont.COURIERBOLD), 60);
147     stampstyle.setTextAlign(PDFStyle.TEXTALIGN_CENTER);
148     stampstyle.setLineColor(new Color JavaDoc(0x00FF0000));
149     stampstyle.setFontStyle(PDFStyle.FONTSTYLE_OUTLINE); // Set outlining
150
}
151
152     static void stamp3(PDFPage page, float midx, float midy)
153     {
154     page.save();
155     page.setStyle(stampstyle);
156     page.rotate(midx, midy, -20);
157     page.drawText("Confidential", midx, midy);
158     page.restore();
159     }
160
161
162     //------------------------------------------------------------------------
163
// The fourth type of stamp - like stamp3 but instead of outlined text, we
164
// add translucent text.
165
//
166
// Pros: * Difficult to remove even with an API and a knowledge of PDF.
167
// * Can use any text you like, in any layout or font.
168
//
169
// Cons: * Translucency only works in Acrobat 5 or later
170
// * Can be obscured by an annotations displayed above it.
171
//
172
static void initstamp4()
173     {
174     stampstyle = new PDFStyle();
175     stampstyle.setFont(new StandardFont(StandardFont.COURIERBOLD), 60);
176     stampstyle.setTextAlign(PDFStyle.TEXTALIGN_CENTER);
177     stampstyle.setFillColor(new Color JavaDoc(0x80FF0000, true)); // Set alpha value
178
}
179
180     static void stamp4(PDFPage page, float midx, float midy)
181     {
182     page.save();
183     page.setStyle(stampstyle);
184     page.rotate(midx, midy, -20);
185     page.drawText("Confidential", midx, midy);
186     page.restore();
187     }
188 }
189
Popular Tags