1 | /* | |
2 | * $Id: PdfAnnotationsImp.java 3912 2009-04-26 08:38:15Z 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; | |
53 | import java.net.URL; | |
54 | import java.util.ArrayList; | |
55 | import java.util.List; | |
56 | import java.util.Map; | |
57 | ||
58 | import com.lowagie.text.Annotation; | |
59 | ||
60 | import com.lowagie.text.Rectangle; | |
61 | import com.lowagie.text.ExceptionConverter; | |
62 | import com.lowagie.text.pdf.*; | |
63 | ||
64 | public class PdfAnnotationsImp { | |
65 | ||
66 | /** | |
67 | * This is the AcroForm object for the complete document. | |
68 | */ | |
69 | protected PdfAcroForm acroForm; | |
70 | ||
71 | /** | |
72 | * This is the array containing the references to annotations | |
73 | * that were added to the document. | |
74 | */ | |
75 | protected List<PdfAnnotation> annotations; | |
76 | | |
77 | /** | |
78 | * This is an array containing references to some delayed annotations | |
79 | * (that were added for a page that doesn't exist yet). | |
80 | */ | |
81 | protected List<PdfAnnotation> delayedAnnotations = new ArrayList<>(); | |
82 | | |
83 | | |
84 | public PdfAnnotationsImp(PdfWriter writer) { | |
85 | acroForm = new PdfAcroForm(writer); | |
86 | } | |
87 | | |
88 | /** | |
89 | * Checks if the AcroForm is valid. | |
90 | */ | |
91 | public boolean hasValidAcroForm() { | |
92 | return acroForm.isValid(); | |
93 | } | |
94 | | |
95 | /** | |
96 | * Gets the AcroForm object. | |
97 | * @return the PdfAcroform object of the PdfDocument | |
98 | */ | |
99 | public PdfAcroForm getAcroForm() { | |
100 | return acroForm; | |
101 | } | |
102 | | |
103 | public void setSigFlags(int f) { | |
104 | acroForm.setSigFlags(f); | |
105 | } | |
106 | | |
107 | public void addCalculationOrder(PdfFormField formField) { | |
108 | acroForm.addCalculationOrder(formField); | |
109 | } | |
110 | | |
111 | public void addAnnotation(PdfAnnotation annot) { | |
112 |
1
1. addAnnotation : negated conditional → NO_COVERAGE |
if (annot.isForm()) { |
113 | PdfFormField field = (PdfFormField)annot; | |
114 |
1
1. addAnnotation : negated conditional → NO_COVERAGE |
if (field.getParent() == null) |
115 |
1
1. addAnnotation : removed call to com/lowagie/text/pdf/internal/PdfAnnotationsImp::addFormFieldRaw → NO_COVERAGE |
addFormFieldRaw(field); |
116 | } | |
117 | else | |
118 | annotations.add(annot); | |
119 | } | |
120 | | |
121 | public void addPlainAnnotation(PdfAnnotation annot) { | |
122 | annotations.add(annot); | |
123 | } | |
124 | | |
125 | void addFormFieldRaw(PdfFormField field) { | |
126 | annotations.add(field); | |
127 | List<PdfFormField> kids = field.getKids(); | |
128 |
1
1. addFormFieldRaw : negated conditional → NO_COVERAGE |
if (kids != null) { |
129 | for (PdfFormField kid : kids) { | |
130 |
1
1. addFormFieldRaw : removed call to com/lowagie/text/pdf/internal/PdfAnnotationsImp::addFormFieldRaw → NO_COVERAGE |
addFormFieldRaw(kid); |
131 | } | |
132 | } | |
133 | } | |
134 | | |
135 | public boolean hasUnusedAnnotations() { | |
136 |
2
1. hasUnusedAnnotations : negated conditional → NO_COVERAGE 2. hasUnusedAnnotations : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return !annotations.isEmpty(); |
137 | } | |
138 | ||
139 | public void resetAnnotations() { | |
140 | annotations = delayedAnnotations; | |
141 | delayedAnnotations = new ArrayList<>(); | |
142 | } | |
143 | | |
144 | public PdfArray rotateAnnotations(PdfWriter writer, Rectangle pageSize) { | |
145 | PdfArray array = new PdfArray(); | |
146 |
1
1. rotateAnnotations : Replaced integer modulus with multiplication → NO_COVERAGE |
int rotation = pageSize.getRotation() % 360; |
147 | int currentPage = writer.getCurrentPageNumber(); | |
148 | for (Object annotation : annotations) { | |
149 | PdfAnnotation dic = (PdfAnnotation) annotation; | |
150 | int page = dic.getPlaceInPage(); | |
151 |
2
1. rotateAnnotations : changed conditional boundary → NO_COVERAGE 2. rotateAnnotations : negated conditional → NO_COVERAGE |
if (page > currentPage) { |
152 | delayedAnnotations.add(dic); | |
153 | continue; | |
154 | } | |
155 |
1
1. rotateAnnotations : negated conditional → NO_COVERAGE |
if (dic.isForm()) { |
156 |
1
1. rotateAnnotations : negated conditional → NO_COVERAGE |
if (!dic.isUsed()) { |
157 | Map<PdfTemplate, Object> templates = dic.getTemplates(); | |
158 |
1
1. rotateAnnotations : negated conditional → NO_COVERAGE |
if (templates != null) |
159 |
1
1. rotateAnnotations : removed call to com/lowagie/text/pdf/PdfAcroForm::addFieldTemplates → NO_COVERAGE |
acroForm.addFieldTemplates(templates); |
160 | } | |
161 | PdfFormField field = (PdfFormField) dic; | |
162 |
1
1. rotateAnnotations : negated conditional → NO_COVERAGE |
if (field.getParent() == null) |
163 |
1
1. rotateAnnotations : removed call to com/lowagie/text/pdf/PdfAcroForm::addDocumentField → NO_COVERAGE |
acroForm.addDocumentField(field.getIndirectReference()); |
164 | } | |
165 |
1
1. rotateAnnotations : negated conditional → NO_COVERAGE |
if (dic.isAnnotation()) { |
166 | array.add(dic.getIndirectReference()); | |
167 |
1
1. rotateAnnotations : negated conditional → NO_COVERAGE |
if (!dic.isUsed()) { |
168 | PdfRectangle rect = (PdfRectangle) dic.get(PdfName.RECT); | |
169 |
1
1. rotateAnnotations : negated conditional → NO_COVERAGE |
if (rect != null) { |
170 | switch (rotation) { | |
171 | case 90: | |
172 |
1
1. rotateAnnotations : removed call to com/lowagie/text/pdf/PdfAnnotation::put → NO_COVERAGE |
dic.put(PdfName.RECT, new PdfRectangle( |
173 |
1
1. rotateAnnotations : Replaced float subtraction with addition → NO_COVERAGE |
pageSize.getTop() - rect.bottom(), |
174 | rect.left(), | |
175 |
1
1. rotateAnnotations : Replaced float subtraction with addition → NO_COVERAGE |
pageSize.getTop() - rect.top(), |
176 | rect.right())); | |
177 | break; | |
178 | case 180: | |
179 |
1
1. rotateAnnotations : removed call to com/lowagie/text/pdf/PdfAnnotation::put → NO_COVERAGE |
dic.put(PdfName.RECT, new PdfRectangle( |
180 |
1
1. rotateAnnotations : Replaced float subtraction with addition → NO_COVERAGE |
pageSize.getRight() - rect.left(), |
181 |
1
1. rotateAnnotations : Replaced float subtraction with addition → NO_COVERAGE |
pageSize.getTop() - rect.bottom(), |
182 |
1
1. rotateAnnotations : Replaced float subtraction with addition → NO_COVERAGE |
pageSize.getRight() - rect.right(), |
183 |
1
1. rotateAnnotations : Replaced float subtraction with addition → NO_COVERAGE |
pageSize.getTop() - rect.top())); |
184 | break; | |
185 | case 270: | |
186 |
1
1. rotateAnnotations : removed call to com/lowagie/text/pdf/PdfAnnotation::put → NO_COVERAGE |
dic.put(PdfName.RECT, new PdfRectangle( |
187 | rect.bottom(), | |
188 |
1
1. rotateAnnotations : Replaced float subtraction with addition → NO_COVERAGE |
pageSize.getRight() - rect.left(), |
189 | rect.top(), | |
190 |
1
1. rotateAnnotations : Replaced float subtraction with addition → NO_COVERAGE |
pageSize.getRight() - rect.right())); |
191 | break; | |
192 | } | |
193 | } | |
194 | } | |
195 | } | |
196 |
1
1. rotateAnnotations : negated conditional → NO_COVERAGE |
if (!dic.isUsed()) { |
197 |
1
1. rotateAnnotations : removed call to com/lowagie/text/pdf/PdfAnnotation::setUsed → NO_COVERAGE |
dic.setUsed(); |
198 | try { | |
199 | writer.addToBody(dic, dic.getIndirectReference()); | |
200 | } catch (IOException e) { | |
201 | throw new ExceptionConverter(e); | |
202 | } | |
203 | } | |
204 | } | |
205 |
1
1. rotateAnnotations : mutated return of Object value for com/lowagie/text/pdf/internal/PdfAnnotationsImp::rotateAnnotations to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return array; |
206 | } | |
207 | | |
208 | public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException { | |
209 | switch(annot.annotationType()) { | |
210 | case Annotation.URL_NET: | |
211 |
1
1. convertAnnotation : mutated return of Object value for com/lowagie/text/pdf/internal/PdfAnnotationsImp::convertAnnotation to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL) annot.attributes().get(Annotation.URL))); |
212 | case Annotation.URL_AS_STRING: | |
213 |
1
1. convertAnnotation : mutated return of Object value for com/lowagie/text/pdf/internal/PdfAnnotationsImp::convertAnnotation to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE))); |
214 | case Annotation.FILE_DEST: | |
215 |
1
1. convertAnnotation : mutated return of Object value for com/lowagie/text/pdf/internal/PdfAnnotationsImp::convertAnnotation to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), (String) annot.attributes().get(Annotation.DESTINATION))); |
216 | case Annotation.SCREEN: | |
217 | boolean[] sparams = (boolean[]) annot.attributes().get(Annotation.PARAMETERS); | |
218 | String fname = (String) annot.attributes().get(Annotation.FILE); | |
219 | String mimetype = (String) annot.attributes().get(Annotation.MIMETYPE); | |
220 | PdfFileSpecification fs; | |
221 |
1
1. convertAnnotation : negated conditional → NO_COVERAGE |
if (sparams[0]) |
222 | fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null); | |
223 | else | |
224 | fs = PdfFileSpecification.fileExtern(writer, fname); | |
225 |
1
1. convertAnnotation : mutated return of Object value for com/lowagie/text/pdf/internal/PdfAnnotationsImp::convertAnnotation to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()), |
226 | fname, fs, mimetype, sparams[1]); | |
227 | case Annotation.FILE_PAGE: | |
228 |
1
1. convertAnnotation : mutated return of Object value for com/lowagie/text/pdf/internal/PdfAnnotationsImp::convertAnnotation to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), (Integer) annot.attributes().get(Annotation.PAGE))); |
229 | case Annotation.NAMED_DEST: | |
230 |
1
1. convertAnnotation : mutated return of Object value for com/lowagie/text/pdf/internal/PdfAnnotationsImp::convertAnnotation to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((Integer) annot.attributes().get(Annotation.NAMED))); |
231 | case Annotation.LAUNCH: | |
232 |
1
1. convertAnnotation : mutated return of Object value for com/lowagie/text/pdf/internal/PdfAnnotationsImp::convertAnnotation to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.APPLICATION),(String) annot.attributes().get(Annotation.PARAMETERS),(String) annot.attributes().get(Annotation.OPERATION),(String) annot.attributes().get(Annotation.DEFAULTDIR))); |
233 | default: | |
234 |
1
1. convertAnnotation : mutated return of Object value for com/lowagie/text/pdf/internal/PdfAnnotationsImp::convertAnnotation to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
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)); |
235 | } | |
236 | } | |
237 | } | |
Mutations | ||
112 |
1.1 |
|
114 |
1.1 |
|
115 |
1.1 |
|
128 |
1.1 |
|
130 |
1.1 |
|
136 |
1.1 2.2 |
|
146 |
1.1 |
|
151 |
1.1 2.2 |
|
155 |
1.1 |
|
156 |
1.1 |
|
158 |
1.1 |
|
159 |
1.1 |
|
162 |
1.1 |
|
163 |
1.1 |
|
165 |
1.1 |
|
167 |
1.1 |
|
169 |
1.1 |
|
172 |
1.1 |
|
173 |
1.1 |
|
175 |
1.1 |
|
179 |
1.1 |
|
180 |
1.1 |
|
181 |
1.1 |
|
182 |
1.1 |
|
183 |
1.1 |
|
186 |
1.1 |
|
188 |
1.1 |
|
190 |
1.1 |
|
196 |
1.1 |
|
197 |
1.1 |
|
205 |
1.1 |
|
211 |
1.1 |
|
213 |
1.1 |
|
215 |
1.1 |
|
221 |
1.1 |
|
225 |
1.1 |
|
228 |
1.1 |
|
230 |
1.1 |
|
232 |
1.1 |
|
234 |
1.1 |