1 | /* | |
2 | * Copyright 2005 by Bruno Lowagie | |
3 | * | |
4 | * The contents of this file are subject to the Mozilla Public License Version 1.1 | |
5 | * (the "License"); you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
7 | * | |
8 | * Software distributed under the License is distributed on an "AS IS" basis, | |
9 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
10 | * for the specific language governing rights and limitations under the License. | |
11 | * | |
12 | * The Original Code is 'iText, a free JAVA-PDF library'. | |
13 | * | |
14 | * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by | |
15 | * the Initial Developer are Copyright (C) 1999-2005 by Bruno Lowagie. | |
16 | * All Rights Reserved. | |
17 | * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer | |
18 | * are Copyright (C) 2000-2005 by Paulo Soares. All Rights Reserved. | |
19 | * | |
20 | * Contributor(s): all the names of the contributors are added in the source code | |
21 | * where applicable. | |
22 | * | |
23 | * Alternatively, the contents of this file may be used under the terms of the | |
24 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
25 | * provisions of LGPL are applicable instead of those above. If you wish to | |
26 | * allow use of your version of this file only under the terms of the LGPL | |
27 | * License and not to allow others to use your version of this file under | |
28 | * the MPL, indicate your decision by deleting the provisions above and | |
29 | * replace them with the notice and other provisions required by the LGPL. | |
30 | * If you do not delete the provisions above, a recipient may use your version | |
31 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
32 | * | |
33 | * This library is free software; you can redistribute it and/or modify it | |
34 | * under the terms of the MPL as stated above or under the terms of the GNU | |
35 | * Library General Public License as published by the Free Software Foundation; | |
36 | * either version 2 of the License, or any later version. | |
37 | * | |
38 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
39 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
40 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
41 | * details. | |
42 | * | |
43 | * If you didn't download this code from the following link, you should check if | |
44 | * you aren't using an obsolete version: | |
45 | * http://www.lowagie.com/iText/ | |
46 | */ | |
47 | package com.lowagie.text.pdf.events; | |
48 | ||
49 | import java.io.IOException; | |
50 | import java.util.HashMap; | |
51 | import com.lowagie.text.error_messages.MessageLocalization; | |
52 | ||
53 | import com.lowagie.text.Document; | |
54 | import com.lowagie.text.DocumentException; | |
55 | ||
56 | import com.lowagie.text.Rectangle; | |
57 | import com.lowagie.text.ExceptionConverter; | |
58 | import com.lowagie.text.pdf.PdfContentByte; | |
59 | import com.lowagie.text.pdf.PdfFormField; | |
60 | import com.lowagie.text.pdf.PdfName; | |
61 | import com.lowagie.text.pdf.PdfPCell; | |
62 | import com.lowagie.text.pdf.PdfPCellEvent; | |
63 | import com.lowagie.text.pdf.PdfPageEventHelper; | |
64 | import com.lowagie.text.pdf.PdfRectangle; | |
65 | import com.lowagie.text.pdf.PdfWriter; | |
66 | import com.lowagie.text.pdf.TextField; | |
67 | ||
68 | /** | |
69 | * Class that can be used to position AcroForm fields. | |
70 | */ | |
71 | public class FieldPositioningEvents extends PdfPageEventHelper implements PdfPCellEvent { | |
72 | ||
73 | /** | |
74 | * Keeps a map with fields that are to be positioned in inGenericTag. | |
75 | */ | |
76 | protected HashMap genericChunkFields = new HashMap(); | |
77 | ||
78 | /** | |
79 | * Keeps the form field that is to be positioned in a cellLayout event. | |
80 | */ | |
81 | protected PdfFormField cellField = null; | |
82 | | |
83 | /** | |
84 | * The PdfWriter to use when a field has to added in a cell event. | |
85 | */ | |
86 | protected PdfWriter fieldWriter = null; | |
87 | /** | |
88 | * The PdfFormField that is the parent of the field added in a cell event. | |
89 | */ | |
90 | protected PdfFormField parent = null; | |
91 | | |
92 | /** Creates a new event. This constructor will be used if you need to position fields with Chunk objects. */ | |
93 | public FieldPositioningEvents() {} | |
94 | | |
95 | /** Some extra padding that will be taken into account when defining the widget. */ | |
96 | public float padding; | |
97 | | |
98 | /** | |
99 | * Add a PdfFormField that has to be tied to a generic Chunk. | |
100 | */ | |
101 | public void addField(String text, PdfFormField field) { | |
102 | genericChunkFields.put(text, field); | |
103 | } | |
104 | | |
105 | /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. */ | |
106 | public FieldPositioningEvents(PdfWriter writer, PdfFormField field) { | |
107 | this.cellField = field; | |
108 | this.fieldWriter = writer; | |
109 | } | |
110 | | |
111 | /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. */ | |
112 | public FieldPositioningEvents(PdfFormField parent, PdfFormField field) { | |
113 | this.cellField = field; | |
114 | this.parent = parent; | |
115 | } | |
116 | | |
117 | /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. | |
118 | * @throws DocumentException | |
119 | * @throws IOException*/ | |
120 | public FieldPositioningEvents(PdfWriter writer, String text) throws IOException, DocumentException { | |
121 | this.fieldWriter = writer; | |
122 | TextField tf = new TextField(writer, new Rectangle(0, 0), text); | |
123 |
1
1. |
tf.setFontSize(14); |
124 | cellField = tf.getTextField(); | |
125 | } | |
126 | | |
127 | /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. | |
128 | * @throws DocumentException | |
129 | * @throws IOException*/ | |
130 | public FieldPositioningEvents(PdfWriter writer, PdfFormField parent, String text) throws IOException, DocumentException { | |
131 | this.parent = parent; | |
132 | TextField tf = new TextField(writer, new Rectangle(0, 0), text); | |
133 |
1
1. |
tf.setFontSize(14); |
134 | cellField = tf.getTextField(); | |
135 | } | |
136 | ||
137 | /** | |
138 | * @param padding The padding to set. | |
139 | */ | |
140 | public void setPadding(float padding) { | |
141 | this.padding = padding; | |
142 | } | |
143 | | |
144 | /** | |
145 | * @param parent The parent to set. | |
146 | */ | |
147 | public void setParent(PdfFormField parent) { | |
148 | this.parent = parent; | |
149 | } | |
150 | /** | |
151 | * @see com.lowagie.text.pdf.PdfPageEvent#onGenericTag(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document, com.lowagie.text.Rectangle, java.lang.String) | |
152 | */ | |
153 | public void onGenericTag(PdfWriter writer, Document document, | |
154 | Rectangle rect, String text) { | |
155 |
2
1. onGenericTag : Replaced float subtraction with addition → NO_COVERAGE 2. onGenericTag : removed call to com/lowagie/text/Rectangle::setBottom → NO_COVERAGE |
rect.setBottom(rect.getBottom() - 3); |
156 | PdfFormField field = (PdfFormField) genericChunkFields.get(text); | |
157 |
1
1. onGenericTag : negated conditional → NO_COVERAGE |
if (field == null) { |
158 | TextField tf = new TextField(writer, new Rectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)), text); | |
159 |
1
1. onGenericTag : removed call to com/lowagie/text/pdf/TextField::setFontSize → NO_COVERAGE |
tf.setFontSize(14); |
160 | try { | |
161 | field = tf.getTextField(); | |
162 | } catch (Exception e) { | |
163 | throw new ExceptionConverter(e); | |
164 | } | |
165 | } | |
166 | else { | |
167 |
1
1. onGenericTag : removed call to com/lowagie/text/pdf/PdfFormField::put → NO_COVERAGE |
field.put(PdfName.RECT, new PdfRectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding))); |
168 | } | |
169 |
1
1. onGenericTag : negated conditional → NO_COVERAGE |
if (parent == null) |
170 |
1
1. onGenericTag : removed call to com/lowagie/text/pdf/PdfWriter::addAnnotation → NO_COVERAGE |
writer.addAnnotation(field); |
171 | else | |
172 |
1
1. onGenericTag : removed call to com/lowagie/text/pdf/PdfFormField::addKid → NO_COVERAGE |
parent.addKid(field); |
173 | } | |
174 | ||
175 | /** | |
176 | * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[]) | |
177 | */ | |
178 | public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) { | |
179 |
3
1. cellLayout : negated conditional → NO_COVERAGE 2. cellLayout : negated conditional → NO_COVERAGE 3. cellLayout : negated conditional → NO_COVERAGE |
if (cellField == null || (fieldWriter == null && parent == null)) throw new IllegalArgumentException(MessageLocalization.getComposedMessage("you.have.used.the.wrong.constructor.for.this.fieldpositioningevents.class")); |
180 |
1
1. cellLayout : removed call to com/lowagie/text/pdf/PdfFormField::put → NO_COVERAGE |
cellField.put(PdfName.RECT, new PdfRectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding))); |
181 |
1
1. cellLayout : negated conditional → NO_COVERAGE |
if (parent == null) |
182 |
1
1. cellLayout : removed call to com/lowagie/text/pdf/PdfWriter::addAnnotation → NO_COVERAGE |
fieldWriter.addAnnotation(cellField); |
183 | else | |
184 |
1
1. cellLayout : removed call to com/lowagie/text/pdf/PdfFormField::addKid → NO_COVERAGE |
parent.addKid(cellField); |
185 | } | |
186 | } | |
Mutations | ||
123 |
1.1 |
|
133 |
1.1 |
|
155 |
1.1 2.2 |
|
157 |
1.1 |
|
159 |
1.1 |
|
167 |
1.1 |
|
169 |
1.1 |
|
170 |
1.1 |
|
172 |
1.1 |
|
179 |
1.1 2.2 3.3 |
|
180 |
1.1 |
|
181 |
1.1 |
|
182 |
1.1 |
|
184 |
1.1 |