KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > internal > PdfAnnotationsImp


1 /*
2  * $Id: PdfAnnotationsImp.java 2742 2007-05-08 13:04:56Z blowagie $
3  *
4  * Copyright 2006 Bruno Lowagie
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text.pdf.internal;
51
52 import java.io.IOException JavaDoc;
53 import java.net.URL JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.HashMap JavaDoc;
56
57 import com.lowagie.text.Annotation;
58 import com.lowagie.text.ExceptionConverter;
59 import com.lowagie.text.Rectangle;
60 import com.lowagie.text.pdf.PdfAcroForm;
61 import com.lowagie.text.pdf.PdfAction;
62 import com.lowagie.text.pdf.PdfAnnotation;
63 import com.lowagie.text.pdf.PdfArray;
64 import com.lowagie.text.pdf.PdfFileSpecification;
65 import com.lowagie.text.pdf.PdfFormField;
66 import com.lowagie.text.pdf.PdfName;
67 import com.lowagie.text.pdf.PdfObject;
68 import com.lowagie.text.pdf.PdfRectangle;
69 import com.lowagie.text.pdf.PdfString;
70 import com.lowagie.text.pdf.PdfWriter;
71
72 public class PdfAnnotationsImp {
73
74     /**
75      * This is the AcroForm object for the complete document.
76      */

77     protected PdfAcroForm acroForm;
78
79     /**
80      * This is the array containing the references to annotations
81      * that were added to the document.
82      */

83     protected ArrayList JavaDoc annotations;
84     
85     /**
86      * This is an array containg references to some delayed annotations
87      * (that were added for a page that doesn't exist yet).
88      */

89     protected ArrayList JavaDoc delayedAnnotations = new ArrayList JavaDoc();
90     
91     
92     public PdfAnnotationsImp(PdfWriter writer) {
93         acroForm = new PdfAcroForm(writer);
94     }
95     
96     /**
97      * Checks if the AcroForm is valid.
98      */

99     public boolean hasValidAcroForm() {
100         return acroForm.isValid();
101     }
102     
103     /**
104      * Gets the AcroForm object.
105      * @return the PdfAcroform object of the PdfDocument
106      */

107     public PdfAcroForm getAcroForm() {
108         return acroForm;
109     }
110     
111     public void setSigFlags(int f) {
112         acroForm.setSigFlags(f);
113     }
114     
115     public void addCalculationOrder(PdfFormField formField) {
116         acroForm.addCalculationOrder(formField);
117     }
118     
119     public void addAnnotation(PdfAnnotation annot) {
120         if (annot.isForm()) {
121             PdfFormField field = (PdfFormField)annot;
122             if (field.getParent() == null)
123                 addFormFieldRaw(field);
124         }
125         else
126             annotations.add(annot);
127     }
128     
129     public void addPlainAnnotation(PdfAnnotation annot) {
130         annotations.add(annot);
131     }
132     
133     void addFormFieldRaw(PdfFormField field) {
134         annotations.add(field);
135         ArrayList JavaDoc kids = field.getKids();
136         if (kids != null) {
137             for (int k = 0; k < kids.size(); ++k)
138                 addFormFieldRaw((PdfFormField)kids.get(k));
139         }
140     }
141     
142     public boolean hasUnusedAnnotations() {
143         return !annotations.isEmpty();
144     }
145
146     public void resetAnnotations() {
147         annotations = delayedAnnotations;
148         delayedAnnotations = new ArrayList JavaDoc();
149     }
150     
151     public PdfArray rotateAnnotations(PdfWriter writer, Rectangle pageSize) {
152         PdfArray array = new PdfArray();
153         int rotation = pageSize.getRotation() % 360;
154         int currentPage = writer.getCurrentPageNumber();
155         for (int k = 0; k < annotations.size(); ++k) {
156             PdfAnnotation dic = (PdfAnnotation)annotations.get(k);
157             int page = dic.getPlaceInPage();
158             if (page > currentPage) {
159                 delayedAnnotations.add(dic);
160                 continue;
161             }
162             if (dic.isForm()) {
163                 if (!dic.isUsed()) {
164                     HashMap JavaDoc templates = dic.getTemplates();
165                     if (templates != null)
166                         acroForm.addFieldTemplates(templates);
167                 }
168                 PdfFormField field = (PdfFormField)dic;
169                 if (field.getParent() == null)
170                     acroForm.addDocumentField(field.getIndirectReference());
171             }
172             if (dic.isAnnotation()) {
173                 array.add(dic.getIndirectReference());
174                 if (!dic.isUsed()) {
175                     PdfRectangle rect = (PdfRectangle)dic.get(PdfName.RECT);
176                     if (rect != null) {
177                         switch (rotation) {
178                             case 90:
179                                 dic.put(PdfName.RECT, new PdfRectangle(
180                                         pageSize.getTop() - rect.bottom(),
181                                         rect.left(),
182                                         pageSize.getTop() - rect.top(),
183                                         rect.right()));
184                                 break;
185                             case 180:
186                                 dic.put(PdfName.RECT, new PdfRectangle(
187                                         pageSize.getRight() - rect.left(),
188                                         pageSize.getTop() - rect.bottom(),
189                                         pageSize.getRight() - rect.right(),
190                                         pageSize.getTop() - rect.top()));
191                                 break;
192                             case 270:
193                                 dic.put(PdfName.RECT, new PdfRectangle(
194                                         rect.bottom(),
195                                         pageSize.getRight() - rect.left(),
196                                         rect.top(),
197                                         pageSize.getRight() - rect.right()));
198                                 break;
199                         }
200                     }
201                 }
202             }
203             if (!dic.isUsed()) {
204                 dic.setUsed();
205                 try {
206                     writer.addToBody(dic, dic.getIndirectReference());
207                 }
208                 catch (IOException JavaDoc e) {
209                     throw new ExceptionConverter(e);
210                 }
211             }
212         }
213         return array;
214     }
215     
216     public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException JavaDoc {
217         switch(annot.annotationType()) {
218            case Annotation.URL_NET:
219                return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL JavaDoc) annot.attributes().get(Annotation.URL)));
220            case Annotation.URL_AS_STRING:
221                return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String JavaDoc) annot.attributes().get(Annotation.FILE)));
222            case Annotation.FILE_DEST:
223                return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String JavaDoc) annot.attributes().get(Annotation.FILE), (String JavaDoc) annot.attributes().get(Annotation.DESTINATION)));
224            case Annotation.SCREEN:
225                boolean sparams[] = (boolean[])annot.attributes().get(Annotation.PARAMETERS);
226                String JavaDoc fname = (String JavaDoc) annot.attributes().get(Annotation.FILE);
227                String JavaDoc mimetype = (String JavaDoc) annot.attributes().get(Annotation.MIMETYPE);
228                PdfFileSpecification fs;
229                if (sparams[0])
230                    fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null);
231                else
232                    fs = PdfFileSpecification.fileExtern(writer, fname);
233                PdfAnnotation ann = PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()),
234                        fname, fs, mimetype, sparams[1]);
235                return ann;
236            case Annotation.FILE_PAGE:
237                return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String JavaDoc) annot.attributes().get(Annotation.FILE), ((Integer JavaDoc) annot.attributes().get(Annotation.PAGE)).intValue()));
238            case Annotation.NAMED_DEST:
239                return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction(((Integer JavaDoc) annot.attributes().get(Annotation.NAMED)).intValue()));
240            case Annotation.LAUNCH:
241                return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String JavaDoc) annot.attributes().get(Annotation.APPLICATION),(String JavaDoc) annot.attributes().get(Annotation.PARAMETERS),(String JavaDoc) annot.attributes().get(Annotation.OPERATION),(String JavaDoc) annot.attributes().get(Annotation.DEFAULTDIR)));
242            default:
243                return new PdfAnnotation(writer, defaultRect.getLeft(), defaultRect.getBottom(), defaultRect.getRight(), defaultRect.getTop(), new PdfString(annot.title(), PdfObject.TEXT_UNICODE), new PdfString(annot.content(), PdfObject.TEXT_UNICODE));
244        }
245    }
246 }
247
Popular Tags