001: /*
002: * $Id: PdfAnnotationsImp.java 2742 2007-05-08 13:04:56Z blowagie $
003: *
004: * Copyright 2006 Bruno Lowagie
005: *
006: * The contents of this file are subject to the Mozilla Public License Version 1.1
007: * (the "License"); you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the License.
013: *
014: * The Original Code is 'iText, a free JAVA-PDF library'.
015: *
016: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
017: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
018: * All Rights Reserved.
019: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
020: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
021: *
022: * Contributor(s): all the names of the contributors are added in the source code
023: * where applicable.
024: *
025: * Alternatively, the contents of this file may be used under the terms of the
026: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
027: * provisions of LGPL are applicable instead of those above. If you wish to
028: * allow use of your version of this file only under the terms of the LGPL
029: * License and not to allow others to use your version of this file under
030: * the MPL, indicate your decision by deleting the provisions above and
031: * replace them with the notice and other provisions required by the LGPL.
032: * If you do not delete the provisions above, a recipient may use your version
033: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
034: *
035: * This library is free software; you can redistribute it and/or modify it
036: * under the terms of the MPL as stated above or under the terms of the GNU
037: * Library General Public License as published by the Free Software Foundation;
038: * either version 2 of the License, or any later version.
039: *
040: * This library is distributed in the hope that it will be useful, but WITHOUT
041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
042: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
043: * details.
044: *
045: * If you didn't download this code from the following link, you should check if
046: * you aren't using an obsolete version:
047: * http://www.lowagie.com/iText/
048: */
049:
050: package com.lowagie.text.pdf.internal;
051:
052: import java.io.IOException;
053: import java.net.URL;
054: import java.util.ArrayList;
055: import java.util.HashMap;
056:
057: import com.lowagie.text.Annotation;
058: import com.lowagie.text.ExceptionConverter;
059: import com.lowagie.text.Rectangle;
060: import com.lowagie.text.pdf.PdfAcroForm;
061: import com.lowagie.text.pdf.PdfAction;
062: import com.lowagie.text.pdf.PdfAnnotation;
063: import com.lowagie.text.pdf.PdfArray;
064: import com.lowagie.text.pdf.PdfFileSpecification;
065: import com.lowagie.text.pdf.PdfFormField;
066: import com.lowagie.text.pdf.PdfName;
067: import com.lowagie.text.pdf.PdfObject;
068: import com.lowagie.text.pdf.PdfRectangle;
069: import com.lowagie.text.pdf.PdfString;
070: import com.lowagie.text.pdf.PdfWriter;
071:
072: public class PdfAnnotationsImp {
073:
074: /**
075: * This is the AcroForm object for the complete document.
076: */
077: protected PdfAcroForm acroForm;
078:
079: /**
080: * This is the array containing the references to annotations
081: * that were added to the document.
082: */
083: protected ArrayList annotations;
084:
085: /**
086: * This is an array containg references to some delayed annotations
087: * (that were added for a page that doesn't exist yet).
088: */
089: protected ArrayList delayedAnnotations = new ArrayList();
090:
091: public PdfAnnotationsImp(PdfWriter writer) {
092: acroForm = new PdfAcroForm(writer);
093: }
094:
095: /**
096: * Checks if the AcroForm is valid.
097: */
098: public boolean hasValidAcroForm() {
099: return acroForm.isValid();
100: }
101:
102: /**
103: * Gets the AcroForm object.
104: * @return the PdfAcroform object of the PdfDocument
105: */
106: public PdfAcroForm getAcroForm() {
107: return acroForm;
108: }
109:
110: public void setSigFlags(int f) {
111: acroForm.setSigFlags(f);
112: }
113:
114: public void addCalculationOrder(PdfFormField formField) {
115: acroForm.addCalculationOrder(formField);
116: }
117:
118: public void addAnnotation(PdfAnnotation annot) {
119: if (annot.isForm()) {
120: PdfFormField field = (PdfFormField) annot;
121: if (field.getParent() == null)
122: addFormFieldRaw(field);
123: } else
124: annotations.add(annot);
125: }
126:
127: public void addPlainAnnotation(PdfAnnotation annot) {
128: annotations.add(annot);
129: }
130:
131: void addFormFieldRaw(PdfFormField field) {
132: annotations.add(field);
133: ArrayList kids = field.getKids();
134: if (kids != null) {
135: for (int k = 0; k < kids.size(); ++k)
136: addFormFieldRaw((PdfFormField) kids.get(k));
137: }
138: }
139:
140: public boolean hasUnusedAnnotations() {
141: return !annotations.isEmpty();
142: }
143:
144: public void resetAnnotations() {
145: annotations = delayedAnnotations;
146: delayedAnnotations = new ArrayList();
147: }
148:
149: public PdfArray rotateAnnotations(PdfWriter writer,
150: Rectangle pageSize) {
151: PdfArray array = new PdfArray();
152: int rotation = pageSize.getRotation() % 360;
153: int currentPage = writer.getCurrentPageNumber();
154: for (int k = 0; k < annotations.size(); ++k) {
155: PdfAnnotation dic = (PdfAnnotation) annotations.get(k);
156: int page = dic.getPlaceInPage();
157: if (page > currentPage) {
158: delayedAnnotations.add(dic);
159: continue;
160: }
161: if (dic.isForm()) {
162: if (!dic.isUsed()) {
163: HashMap templates = dic.getTemplates();
164: if (templates != null)
165: acroForm.addFieldTemplates(templates);
166: }
167: PdfFormField field = (PdfFormField) dic;
168: if (field.getParent() == null)
169: acroForm.addDocumentField(field
170: .getIndirectReference());
171: }
172: if (dic.isAnnotation()) {
173: array.add(dic.getIndirectReference());
174: if (!dic.isUsed()) {
175: PdfRectangle rect = (PdfRectangle) dic
176: .get(PdfName.RECT);
177: if (rect != null) {
178: switch (rotation) {
179: case 90:
180: dic
181: .put(
182: PdfName.RECT,
183: new PdfRectangle(
184: pageSize.getTop()
185: - rect
186: .bottom(),
187: rect.left(),
188: pageSize.getTop()
189: - rect
190: .top(),
191: rect.right()));
192: break;
193: case 180:
194: dic.put(PdfName.RECT, new PdfRectangle(
195: pageSize.getRight() - rect.left(),
196: pageSize.getTop() - rect.bottom(),
197: pageSize.getRight() - rect.right(),
198: pageSize.getTop() - rect.top()));
199: break;
200: case 270:
201: dic.put(PdfName.RECT, new PdfRectangle(rect
202: .bottom(), pageSize.getRight()
203: - rect.left(), rect.top(), pageSize
204: .getRight()
205: - rect.right()));
206: break;
207: }
208: }
209: }
210: }
211: if (!dic.isUsed()) {
212: dic.setUsed();
213: try {
214: writer.addToBody(dic, dic.getIndirectReference());
215: } catch (IOException e) {
216: throw new ExceptionConverter(e);
217: }
218: }
219: }
220: return array;
221: }
222:
223: public static PdfAnnotation convertAnnotation(PdfWriter writer,
224: Annotation annot, Rectangle defaultRect) throws IOException {
225: switch (annot.annotationType()) {
226: case Annotation.URL_NET:
227: return new PdfAnnotation(writer, annot.llx(), annot.lly(),
228: annot.urx(), annot.ury(), new PdfAction((URL) annot
229: .attributes().get(Annotation.URL)));
230: case Annotation.URL_AS_STRING:
231: return new PdfAnnotation(writer, annot.llx(), annot.lly(),
232: annot.urx(), annot.ury(), new PdfAction(
233: (String) annot.attributes().get(
234: Annotation.FILE)));
235: case Annotation.FILE_DEST:
236: return new PdfAnnotation(writer, annot.llx(), annot.lly(),
237: annot.urx(), annot.ury(), new PdfAction(
238: (String) annot.attributes().get(
239: Annotation.FILE), (String) annot
240: .attributes().get(
241: Annotation.DESTINATION)));
242: case Annotation.SCREEN:
243: boolean sparams[] = (boolean[]) annot.attributes().get(
244: Annotation.PARAMETERS);
245: String fname = (String) annot.attributes().get(
246: Annotation.FILE);
247: String mimetype = (String) annot.attributes().get(
248: Annotation.MIMETYPE);
249: PdfFileSpecification fs;
250: if (sparams[0])
251: fs = PdfFileSpecification.fileEmbedded(writer, fname,
252: fname, null);
253: else
254: fs = PdfFileSpecification.fileExtern(writer, fname);
255: PdfAnnotation ann = PdfAnnotation.createScreen(writer,
256: new Rectangle(annot.llx(), annot.lly(),
257: annot.urx(), annot.ury()), fname, fs,
258: mimetype, sparams[1]);
259: return ann;
260: case Annotation.FILE_PAGE:
261: return new PdfAnnotation(writer, annot.llx(), annot.lly(),
262: annot.urx(), annot.ury(), new PdfAction(
263: (String) annot.attributes().get(
264: Annotation.FILE), ((Integer) annot
265: .attributes().get(Annotation.PAGE))
266: .intValue()));
267: case Annotation.NAMED_DEST:
268: return new PdfAnnotation(writer, annot.llx(), annot.lly(),
269: annot.urx(), annot.ury(), new PdfAction(
270: ((Integer) annot.attributes().get(
271: Annotation.NAMED)).intValue()));
272: case Annotation.LAUNCH:
273: return new PdfAnnotation(writer, annot.llx(), annot.lly(),
274: annot.urx(), annot.ury(), new PdfAction(
275: (String) annot.attributes().get(
276: Annotation.APPLICATION),
277: (String) annot.attributes().get(
278: Annotation.PARAMETERS),
279: (String) annot.attributes().get(
280: Annotation.OPERATION),
281: (String) annot.attributes().get(
282: Annotation.DEFAULTDIR)));
283: default:
284: return new PdfAnnotation(writer, defaultRect.getLeft(),
285: defaultRect.getBottom(), defaultRect.getRight(),
286: defaultRect.getTop(), new PdfString(annot.title(),
287: PdfObject.TEXT_UNICODE), new PdfString(
288: annot.content(), PdfObject.TEXT_UNICODE));
289: }
290: }
291: }
|