Source Code Cross Referenced for FdfWriter.java in  » PDF » pdf-itext » com » lowagie » text » pdf » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » pdf itext » com.lowagie.text.pdf 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2003 by Paulo Soares.
003:         *
004:         * The contents of this file are subject to the Mozilla Public License Version 1.1
005:         * (the "License"); you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007:         *
008:         * Software distributed under the License is distributed on an "AS IS" basis,
009:         * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010:         * for the specific language governing rights and limitations under the License.
011:         *
012:         * The Original Code is 'iText, a free JAVA-PDF library'.
013:         *
014:         * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015:         * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
016:         * All Rights Reserved.
017:         * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018:         * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
019:         *
020:         * Contributor(s): all the names of the contributors are added in the source code
021:         * where applicable.
022:         *
023:         * Alternatively, the contents of this file may be used under the terms of the
024:         * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025:         * provisions of LGPL are applicable instead of those above.  If you wish to
026:         * allow use of your version of this file only under the terms of the LGPL
027:         * License and not to allow others to use your version of this file under
028:         * the MPL, indicate your decision by deleting the provisions above and
029:         * replace them with the notice and other provisions required by the LGPL.
030:         * If you do not delete the provisions above, a recipient may use your version
031:         * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032:         *
033:         * This library is free software; you can redistribute it and/or modify it
034:         * under the terms of the MPL as stated above or under the terms of the GNU
035:         * Library General Public License as published by the Free Software Foundation;
036:         * either version 2 of the License, or any later version.
037:         *
038:         * This library is distributed in the hope that it will be useful, but WITHOUT
039:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040:         * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041:         * details.
042:         *
043:         * If you didn't download this code from the following link, you should check if
044:         * you aren't using an obsolete version:
045:         * http://www.lowagie.com/iText/
046:         */
047:        package com.lowagie.text.pdf;
048:
049:        import java.io.IOException;
050:        import java.io.OutputStream;
051:        import java.util.ArrayList;
052:        import java.util.HashMap;
053:        import java.util.Iterator;
054:        import java.util.Map;
055:        import java.util.StringTokenizer;
056:
057:        import com.lowagie.text.DocWriter;
058:
059:        /** Writes an FDF form.
060:         * @author Paulo Soares (psoares@consiste.pt)
061:         */
062:        public class FdfWriter {
063:            private static final byte[] HEADER_FDF = DocWriter
064:                    .getISOBytes("%FDF-1.2\n%\u00e2\u00e3\u00cf\u00d3\n");
065:            HashMap fields = new HashMap();
066:
067:            /** The PDF file associated with the FDF. */
068:            private String file;
069:
070:            /** Creates a new FdfWriter. */
071:            public FdfWriter() {
072:            }
073:
074:            /** Writes the content to a stream.
075:             * @param os the stream
076:             * @throws IOException on error
077:             */
078:            public void writeTo(OutputStream os) throws IOException {
079:                Wrt wrt = new Wrt(os, this );
080:                wrt.writeTo();
081:            }
082:
083:            boolean setField(String field, PdfObject value) {
084:                HashMap map = fields;
085:                StringTokenizer tk = new StringTokenizer(field, ".");
086:                if (!tk.hasMoreTokens())
087:                    return false;
088:                while (true) {
089:                    String s = tk.nextToken();
090:                    Object obj = map.get(s);
091:                    if (tk.hasMoreTokens()) {
092:                        if (obj == null) {
093:                            obj = new HashMap();
094:                            map.put(s, obj);
095:                            map = (HashMap) obj;
096:                            continue;
097:                        } else if (obj instanceof  HashMap)
098:                            map = (HashMap) obj;
099:                        else
100:                            return false;
101:                    } else {
102:                        if (!(obj instanceof  HashMap)) {
103:                            map.put(s, value);
104:                            return true;
105:                        } else
106:                            return false;
107:                    }
108:                }
109:            }
110:
111:            void iterateFields(HashMap values, HashMap map, String name) {
112:                for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
113:                    Map.Entry entry = (Map.Entry) it.next();
114:                    String s = (String) entry.getKey();
115:                    Object obj = entry.getValue();
116:                    if (obj instanceof  HashMap)
117:                        iterateFields(values, (HashMap) obj, name + "." + s);
118:                    else
119:                        values.put((name + "." + s).substring(1), obj);
120:                }
121:            }
122:
123:            /** Removes the field value.
124:             * @param field the field name
125:             * @return <CODE>true</CODE> if the field was found and removed,
126:             * <CODE>false</CODE> otherwise
127:             */
128:            public boolean removeField(String field) {
129:                HashMap map = fields;
130:                StringTokenizer tk = new StringTokenizer(field, ".");
131:                if (!tk.hasMoreTokens())
132:                    return false;
133:                ArrayList hist = new ArrayList();
134:                while (true) {
135:                    String s = tk.nextToken();
136:                    Object obj = map.get(s);
137:                    if (obj == null)
138:                        return false;
139:                    hist.add(map);
140:                    hist.add(s);
141:                    if (tk.hasMoreTokens()) {
142:                        if (obj instanceof  HashMap)
143:                            map = (HashMap) obj;
144:                        else
145:                            return false;
146:                    } else {
147:                        if (obj instanceof  HashMap)
148:                            return false;
149:                        else
150:                            break;
151:                    }
152:                }
153:                for (int k = hist.size() - 2; k >= 0; k -= 2) {
154:                    map = (HashMap) hist.get(k);
155:                    String s = (String) hist.get(k + 1);
156:                    map.remove(s);
157:                    if (!map.isEmpty())
158:                        break;
159:                }
160:                return true;
161:            }
162:
163:            /** Gets all the fields. The map is keyed by the fully qualified
164:             * field name and the values are <CODE>PdfObject</CODE>.
165:             * @return a map with all the fields
166:             */
167:            public HashMap getFields() {
168:                HashMap values = new HashMap();
169:                iterateFields(values, fields, "");
170:                return values;
171:            }
172:
173:            /** Gets the field value.
174:             * @param field the field name
175:             * @return the field value or <CODE>null</CODE> if not found
176:             */
177:            public String getField(String field) {
178:                HashMap map = fields;
179:                StringTokenizer tk = new StringTokenizer(field, ".");
180:                if (!tk.hasMoreTokens())
181:                    return null;
182:                while (true) {
183:                    String s = tk.nextToken();
184:                    Object obj = map.get(s);
185:                    if (obj == null)
186:                        return null;
187:                    if (tk.hasMoreTokens()) {
188:                        if (obj instanceof  HashMap)
189:                            map = (HashMap) obj;
190:                        else
191:                            return null;
192:                    } else {
193:                        if (obj instanceof  HashMap)
194:                            return null;
195:                        else {
196:                            if (((PdfObject) obj).isString())
197:                                return ((PdfString) obj).toUnicodeString();
198:                            else
199:                                return PdfName.decodeName(obj.toString());
200:                        }
201:                    }
202:                }
203:            }
204:
205:            /** Sets the field value as a name.
206:             * @param field the fully qualified field name
207:             * @param value the value
208:             * @return <CODE>true</CODE> if the value was inserted,
209:             * <CODE>false</CODE> if the name is incompatible with
210:             * an existing field
211:             */
212:            public boolean setFieldAsName(String field, String value) {
213:                return setField(field, new PdfName(value));
214:            }
215:
216:            /** Sets the field value as a string.
217:             * @param field the fully qualified field name
218:             * @param value the value
219:             * @return <CODE>true</CODE> if the value was inserted,
220:             * <CODE>false</CODE> if the name is incompatible with
221:             * an existing field
222:             */
223:            public boolean setFieldAsString(String field, String value) {
224:                return setField(field, new PdfString(value,
225:                        PdfObject.TEXT_UNICODE));
226:            }
227:
228:            /** Sets all the fields from this <CODE>FdfReader</CODE>
229:             * @param fdf the <CODE>FdfReader</CODE>
230:             */
231:            public void setFields(FdfReader fdf) {
232:                HashMap map = fdf.getFields();
233:                for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
234:                    Map.Entry entry = (Map.Entry) it.next();
235:                    String key = (String) entry.getKey();
236:                    PdfDictionary dic = (PdfDictionary) entry.getValue();
237:                    PdfObject v = dic.get(PdfName.V);
238:                    if (v != null) {
239:                        setField(key, v);
240:                    }
241:                }
242:            }
243:
244:            /** Sets all the fields from this <CODE>PdfReader</CODE>
245:             * @param pdf the <CODE>PdfReader</CODE>
246:             */
247:            public void setFields(PdfReader pdf) {
248:                setFields(pdf.getAcroFields());
249:            }
250:
251:            /** Sets all the fields from this <CODE>AcroFields</CODE>
252:             * @param af the <CODE>AcroFields</CODE>
253:             */
254:            public void setFields(AcroFields af) {
255:                for (Iterator it = af.getFields().entrySet().iterator(); it
256:                        .hasNext();) {
257:                    Map.Entry entry = (Map.Entry) it.next();
258:                    String fn = (String) entry.getKey();
259:                    AcroFields.Item item = (AcroFields.Item) entry.getValue();
260:                    PdfDictionary dic = (PdfDictionary) item.merged.get(0);
261:                    PdfObject v = PdfReader.getPdfObjectRelease(dic
262:                            .get(PdfName.V));
263:                    if (v == null)
264:                        continue;
265:                    PdfObject ft = PdfReader.getPdfObjectRelease(dic
266:                            .get(PdfName.FT));
267:                    if (ft == null || PdfName.SIG.equals(ft))
268:                        continue;
269:                    setField(fn, v);
270:                }
271:            }
272:
273:            /** Gets the PDF file name associated with the FDF.
274:             * @return the PDF file name associated with the FDF
275:             */
276:            public String getFile() {
277:                return this .file;
278:            }
279:
280:            /** Sets the PDF file name associated with the FDF.
281:             * @param file the PDF file name associated with the FDF
282:             *
283:             */
284:            public void setFile(String file) {
285:                this .file = file;
286:            }
287:
288:            static class Wrt extends PdfWriter {
289:                private FdfWriter fdf;
290:
291:                Wrt(OutputStream os, FdfWriter fdf) throws IOException {
292:                    super (new PdfDocument(), os);
293:                    this .fdf = fdf;
294:                    this .os.write(HEADER_FDF);
295:                    body = new PdfBody(this );
296:                }
297:
298:                void writeTo() throws IOException {
299:                    PdfDictionary dic = new PdfDictionary();
300:                    dic.put(PdfName.FIELDS, calculate(fdf.fields));
301:                    if (fdf.file != null)
302:                        dic.put(PdfName.F, new PdfString(fdf.file,
303:                                PdfObject.TEXT_UNICODE));
304:                    PdfDictionary fd = new PdfDictionary();
305:                    fd.put(PdfName.FDF, dic);
306:                    PdfIndirectReference ref = addToBody(fd)
307:                            .getIndirectReference();
308:                    os.write(getISOBytes("trailer\n"));
309:                    PdfDictionary trailer = new PdfDictionary();
310:                    trailer.put(PdfName.ROOT, ref);
311:                    trailer.toPdf(null, os);
312:                    os.write(getISOBytes("\n%%EOF\n"));
313:                    os.close();
314:                }
315:
316:                PdfArray calculate(HashMap map) throws IOException {
317:                    PdfArray ar = new PdfArray();
318:                    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
319:                        Map.Entry entry = (Map.Entry) it.next();
320:                        String key = (String) entry.getKey();
321:                        Object v = entry.getValue();
322:                        PdfDictionary dic = new PdfDictionary();
323:                        dic.put(PdfName.T, new PdfString(key,
324:                                PdfObject.TEXT_UNICODE));
325:                        if (v instanceof  HashMap) {
326:                            dic.put(PdfName.KIDS, calculate((HashMap) v));
327:                        } else {
328:                            dic.put(PdfName.V, (PdfObject) v);
329:                        }
330:                        ar.add(dic);
331:                    }
332:                    return ar;
333:                }
334:            }
335:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.