KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > pdmodel > Annotation


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.examples.pdmodel;
32
33 import org.pdfbox.pdmodel.PDDocument;
34 import org.pdfbox.pdmodel.PDPage;
35 import org.pdfbox.pdmodel.common.PDRectangle;
36 import org.pdfbox.pdmodel.edit.PDPageContentStream;
37 import org.pdfbox.pdmodel.font.PDFont;
38 import org.pdfbox.pdmodel.font.PDType1Font;
39 import org.pdfbox.pdmodel.graphics.color.PDGamma;
40 import org.pdfbox.pdmodel.interactive.action.type.PDActionURI;
41 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationLine;
42 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle;
43 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup;
44 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
45 import org.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
46
47
48 import java.util.List JavaDoc;
49
50 /**
51  * This is an example on how to add annotations to pages of a PDF document.
52  *
53  * @author Paul King
54  * @version $Revision: 1.2 $
55  */

56 public class Annotation
57 {
58     private Annotation()
59     {
60         //utility class, should not be instantiated.
61
}
62     
63     /**
64      * This will create a doucument showing various annotations.
65      *
66      * @param args The command line arguments.
67      *
68      * @throws Exception If there is an error parsing the document.
69      */

70     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
71     {
72         if( args.length != 1 )
73         {
74             usage();
75         }
76         else
77         {
78             PDDocument document = new PDDocument();
79             
80             try
81             {
82                 PDPage page = new PDPage();
83                 document.addPage(page);
84                 List JavaDoc annotations = page.getAnnotations();
85
86                 // Setup some basic reusable objects/constants
87
// Annotations themselves can only be used once!
88

89                 float inch = 72;
90                 PDGamma colourRed = new PDGamma();
91                 colourRed.setR(1);
92                 PDGamma colourBlue = new PDGamma();
93                 colourBlue.setB(1);
94                 PDGamma colourBlack = new PDGamma();
95
96                 PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
97                 borderThick.setWidth(inch/12); // 12th inch
98
PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary();
99                 borderThin.setWidth(inch/72); // 1 point
100
PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
101                 borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
102                 borderULine.setWidth(inch/72); // 1 point
103

104                 
105                 float pw = page.getMediaBox().getUpperRightX();
106                 float ph = page.getMediaBox().getUpperRightY();
107                 
108                 
109                 // First add some text, two lines we'll add some annotations to this later
110

111                 
112                 PDFont font = PDType1Font.HELVETICA_BOLD;
113                 
114                 PDPageContentStream contentStream = new PDPageContentStream(document, page);
115                 contentStream.beginText();
116                 contentStream.setFont( font, 18 );
117                 contentStream.moveTextPositionByAmount( inch, ph-inch-18);
118                 contentStream.drawString( "PDFBox" );
119                 contentStream.moveTextPositionByAmount( 0,-(inch/2));
120                 contentStream.drawString( "Click Here" );
121                 contentStream.endText();
122                 
123                 contentStream.close();
124                 
125                 // Now add the markup annotation, a highlight to PDFBox text
126
PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
127                 txtMark.setColour(colourBlue);
128                 txtMark.setConstantOpacity((float)0.2); // Make the highlight 20% transparent
129

130                 // Set the rectangle containing the markup
131

132                 float textWidth = (font.getStringWidth( "PDFBox" )/1000) * 18;
133                 PDRectangle position = new PDRectangle();
134                 position.setLowerLeftX(inch);
135                 position.setLowerLeftY( ph-inch-18 );
136                 position.setUpperRightX(72 + textWidth);
137                 position.setUpperRightY(ph-inch);
138                 txtMark.setRectangle(position);
139                 
140                 // work out the points forming the four corners of the annotations
141
// set out in anti clockwise form (Completely wraps the text)
142
// OK, the below doesn't match that description.
143
// It's what acrobat 7 does and displays properly!
144
float[] quads = new float[8];
145                 
146                 quads[0] = position.getLowerLeftX(); // x1
147
quads[1] = position.getUpperRightY()-2; // y1
148
quads[2] = position.getUpperRightX(); // x2
149
quads[3] = quads[1]; // y2
150
quads[4] = quads[0]; // x3
151
quads[5] = position.getLowerLeftY()-2; // y3
152
quads[6] = quads[2]; // x4
153
quads[7] = quads[5]; // y5
154

155                 txtMark.setQuadPoints(quads);
156                 txtMark.setContents("Highlighted since it's important");
157                 
158                 annotations.add(txtMark);
159                 
160                 // Now add the link annotation, so the clickme works
161
PDAnnotationLink txtLink = new PDAnnotationLink();
162                 txtLink.setBorderStyle(borderULine);
163                 
164                 // Set the rectangle containing the link
165

166                 textWidth = (font.getStringWidth( "Click Here" )/1000) * 18;
167                 position = new PDRectangle();
168                 position.setLowerLeftX(inch);
169                 position.setLowerLeftY( ph-(float)(1.5*inch)-20); // down a couple of points
170
position.setUpperRightX(72 + textWidth);
171                 position.setUpperRightY(ph-(float)(1.5*inch));
172                 txtLink.setRectangle(position);
173                 
174                 // add an action
175
PDActionURI action = new PDActionURI();
176                 action.setURI("http://www.pdfbox.org");
177                 txtLink.setAction(action);
178                 
179                 annotations.add(txtLink);
180                 
181                 
182                 // Now draw a few more annotations
183

184                 PDAnnotationSquareCircle aCircle =
185                     new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_CIRCLE);
186                 aCircle.setContents("Circle Annotation");
187                 aCircle.setInteriorColour(colourRed); // Fill in circle in red
188
aCircle.setColour(colourBlue); // The border itself will be blue
189
aCircle.setBorderStyle(borderThin);
190
191                 // Place the annotation on the page, we'll make this 1" round
192
// 3" down, 1" in on the page
193

194                 position = new PDRectangle();
195                 position.setLowerLeftX(inch);
196                 position.setLowerLeftY(ph-(3*inch)-inch); // 1" height, 3" down
197
position.setUpperRightX(2*inch); // 1" in, 1" width
198
position.setUpperRightY(ph-(3*inch)); // 3" down
199
aCircle.setRectangle(position);
200
201                 // add to the annotations on the page
202
annotations.add(aCircle);
203                 
204                 // Now a square annotation
205

206                 PDAnnotationSquareCircle aSquare =
207                     new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_SQUARE);
208                 aSquare.setContents("Square Annotation");
209                 aSquare.setColour(colourRed); // Outline in red, not setting a fill
210
aSquare.setBorderStyle(borderThick);
211                 
212                 // Place the annotation on the page, we'll make this 1" (72points) square
213
// 3.5" down, 1" in from the right on the page
214

215                 position = new PDRectangle(); // Reuse the variable, but note it's a new object!
216
position.setLowerLeftX(pw-(2*inch)); // 1" in from right, 1" wide
217
position.setLowerLeftY(ph-(float)(3.5*inch) - inch); // 1" height, 3.5" down
218
position.setUpperRightX(pw-inch); // 1" in from right
219
position.setUpperRightY(ph-(float)(3.5*inch)); // 3.5" down
220
aSquare.setRectangle(position);
221
222                 // add to the annotations on the page
223
annotations.add(aSquare);
224                 
225                 // Now we want to draw a line between the two, one end with an open arrow
226

227                 PDAnnotationLine aLine = new PDAnnotationLine();
228                 
229                 aLine.setEndPointEndingStyle( PDAnnotationLine.LE_OPEN_ARROW );
230                 aLine.setContents("Circle->Square");
231                 aLine.setCaption(true); // Make the contents a caption on the line
232

233                 // Set the rectangle containing the line
234

235                 position = new PDRectangle(); // Reuse the variable, but note it's a new object!
236
position.setLowerLeftX(2*inch); // 1" in + width of circle
237
position.setLowerLeftY(ph-(float)(3.5*inch)-inch); // 1" height, 3.5" down
238
position.setUpperRightX(pw-inch-inch); // 1" in from right, and width of square
239
position.setUpperRightY(ph-(3*inch)); // 3" down (top of circle)
240
aLine.setRectangle(position);
241                 
242                 // Now set the line position itself
243
float[] linepos = new float[4];
244                 linepos[0] = 2*inch; // x1 = rhs of circle
245
linepos[1] = ph-(float)(3.5*inch); // y1 halfway down circle
246
linepos[2] = pw-(2*inch); // x2 = lhs of square
247
linepos[3] = ph-(4*inch); // y2 halfway down square
248
aLine.setLine(linepos);
249                 
250                 aLine.setBorderStyle(borderThick);
251                 aLine.setColour(colourBlack);
252                 
253                 // add to the annotations on the page
254
annotations.add(aLine);
255                 
256                 
257                 // Finally all done
258

259                 
260                 document.save( args[0] );
261             }
262             finally
263             {
264                 document.close();
265             }
266         }
267     }
268
269     /**
270      * This will print the usage for this document.
271      */

272     private static void usage()
273     {
274         System.err.println( "Usage: java org.pdfbox.examples.pdmodel.Annotation <output-pdf>" );
275     }
276 }
Popular Tags