KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > persistence > FieldsDoc


1 /**
2  * Copyright (c) 2003, 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.persistence;
32
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36
37 import java.util.Iterator JavaDoc;
38
39 import org.pdfbox.pdfparser.PDFParser;
40
41 import org.pdfbox.pdfwriter.COSWriter;
42
43 import org.pdfbox.cos.COSDocument;
44 import org.pdfbox.cos.COSString;
45 import org.pdfbox.cos.COSBase;
46 import org.pdfbox.cos.COSDictionary;
47 import org.pdfbox.cos.COSObject;
48 import org.pdfbox.cos.COSName;
49
50 import org.pdfbox.exceptions.COSVisitorException;
51
52 /**
53  * This example fills a field in a pdf document and writes it to new destination.
54  *
55  * @author Michael Traut
56  * @version $Revision: 1.5 $
57  */

58 public class FieldsDoc
59 {
60     /**
61      * Constructor.
62      */

63     public FieldsDoc()
64     {
65         super();
66     }
67     /**
68      * fill a field in the pdf.
69      *
70      * @param in The template file
71      * @param out The file to write the PDF to.
72      * @param name The name of the PDF field (FDF field)
73      * @param value The value to be used for the field
74      *
75      * @throws IOException If there is an error writing the data.
76      * @throws COSVisitorException If there is an error generating the PDF document.
77      */

78     public void doIt(String JavaDoc in, String JavaDoc out, String JavaDoc name, String JavaDoc value) throws IOException JavaDoc, COSVisitorException
79     {
80         java.io.InputStream JavaDoc is = null;
81         COSDocument doc = null;
82         OutputStream JavaDoc os = null;
83         COSWriter writer = null;
84         try
85         {
86             is = new java.io.FileInputStream JavaDoc(in);
87             PDFParser parser = new PDFParser(is);
88             parser.parse();
89
90             doc = parser.getDocument();
91
92             setField(doc, new COSString(name), new COSString(value));
93
94             os = new FileOutputStream JavaDoc(out);
95             writer = new COSWriter(os);
96
97             writer.write(doc);
98
99         }
100         finally
101         {
102             is.close();
103             doc.close();
104             os.close();
105             writer.close();
106         }
107     }
108     /**
109      * This will fill a field in a PDF document.
110      * <br />
111      * see usage() for commandline
112      *
113      * @param args command line arguments
114      */

115     public static void main(String JavaDoc[] args)
116     {
117         FieldsDoc app = new FieldsDoc();
118         try
119         {
120             if( args.length != 4 )
121             {
122                 app.usage();
123             }
124             else
125             {
126                 app.doIt( args[0], args[1], args[2], args[3]);
127             }
128         }
129         catch (Exception JavaDoc e)
130         {
131             e.printStackTrace();
132         }
133     }
134     /**
135      * lookup and fill the field.
136      *
137      * todo: this method will go to the pdfmodel package one day
138      *
139      * @param doc the document where the field resides
140      * @param name the name of the PDF Annotation field
141      * @param value The desired value to be used for the field
142      *
143      */

144     public void setField(COSDocument doc, COSString name, COSString value)
145     {
146         for (Iterator JavaDoc i = doc.getObjects().iterator(); i.hasNext();)
147         {
148             COSObject obj = (COSObject) i.next();
149             COSBase base = obj.getObject();
150             if (base instanceof COSDictionary)
151             {
152                 COSDictionary dict = (COSDictionary) base;
153                 COSBase type = dict.getItem(COSName.TYPE);
154                 if (type != null && type.equals(COSName.getPDFName("Annot")))
155                 {
156                     COSBase subtype = dict.getItem(COSName.getPDFName("Subtype"));
157                     if (subtype != null && subtype.equals(COSName.getPDFName("Widget")))
158                     {
159                         // we found the field
160
COSBase fname = dict.getItem(COSName.getPDFName("T"));
161                         if (fname != null && fname.equals(name))
162                         {
163                             dict.setItem(COSName.getPDFName("V"), value);
164                         }
165                     }
166                 }
167             }
168         }
169     }
170     /**
171      * This will print out a message telling how to use this example.
172      */

173     private void usage()
174     {
175         System.err.println( "usage: " + this.getClass().getName() + " <input-file> <output-file> <name> <value>" );
176     }
177 }
Popular Tags