KickJava   Java API By Example, From Geeks To Geeks.

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


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.interactive.annotation.PDAnnotationRubberStamp;
37
38 import java.io.IOException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.List JavaDoc;
41
42 /**
43  * This is an example on how to add annotations to pages of a PDF document.
44  *
45  * @author Paul King
46  * @version $Revision: 1.1 $
47  */

48 public class RubberStamp
49 {
50     private RubberStamp()
51     {
52         //utility class, should not be instantiated.
53
}
54     
55     /**
56      * This will print the documents data.
57      *
58      * @param args The command line arguments.
59      *
60      * @throws Exception If there is an error parsing the document.
61      */

62     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
63     {
64         if( args.length != 2 )
65         {
66             usage();
67         }
68         else
69         {
70             PDDocument document = null;
71             try
72             {
73                 document = PDDocument.load( args[0] );
74                 if( document.isEncrypted() )
75                 {
76                     throw new IOException JavaDoc( "Encrypted documents are not supported for this example" );
77                 }
78                 List JavaDoc allpages = new ArrayList JavaDoc();
79                 document.getDocumentCatalog().getPages().getAllKids(allpages);
80                 
81                 for (int i=0; i < allpages.size(); i++)
82                 {
83                     PDPage apage = (PDPage) allpages.get(i);
84                     List JavaDoc annotations = apage.getAnnotations();
85                     
86                     PDAnnotationRubberStamp rs = new PDAnnotationRubberStamp();
87                     rs.setName(PDAnnotationRubberStamp.NAME_TOP_SECRET);
88                     rs.setRectangle(new PDRectangle(100,100));
89                     rs.setContents("A top secret note");
90                     
91                     annotations.add(rs);
92                 }
93                 
94                 document.save( args[1] );
95             }
96             finally
97             {
98                 if( document != null )
99                 {
100                     document.close();
101                 }
102             }
103         }
104     }
105
106     /**
107      * This will print the usage for this document.
108      */

109     private static void usage()
110     {
111         System.err.println( "Usage: java org.pdfbox.examples.pdmodel.RubberStamp <input-pdf> <output-pdf>" );
112     }
113 }
Popular Tags