Skip to content

Commit ad31477

Browse files
committed
Introduce util class for checkbox and radio drawing.
DEVSIX-1150
1 parent f0335a4 commit ad31477

File tree

2 files changed

+201
-88
lines changed

2 files changed

+201
-88
lines changed

forms/src/main/java/com/itextpdf/forms/fields/PdfFormField.java

Lines changed: 12 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343
*/
4444
package com.itextpdf.forms.fields;
4545

46+
import com.itextpdf.forms.util.DrawingUtil;
4647
import com.itextpdf.io.LogMessageConstant;
4748
import com.itextpdf.io.codec.Base64;
4849
import com.itextpdf.io.font.FontProgram;
@@ -181,70 +182,6 @@ public class PdfFormField extends PdfObjectWrapper<PdfDictionary> {
181182
protected PdfFormXObject form;
182183
protected PdfAConformanceLevel pdfAConformanceLevel;
183184

184-
protected static final String check = "0.8 0 0 0.8 0.3 0.5 cm 0 0 m\n" +
185-
"0.066 -0.026 l\n" +
186-
"0.137 -0.15 l\n" +
187-
"0.259 0.081 0.46 0.391 0.553 0.461 c\n" +
188-
"0.604 0.489 l\n" +
189-
"0.703 0.492 l\n" +
190-
"0.543 0.312 0.255 -0.205 0.154 -0.439 c\n" +
191-
"0.069 -0.399 l\n" +
192-
"0.035 -0.293 -0.039 -0.136 -0.091 -0.057 c\n" +
193-
"h\n" +
194-
"f\n";
195-
196-
protected static final String circle = "1 0 0 1 0.86 0.5 cm 0 0 m\n" +
197-
"0 0.204 -0.166 0.371 -0.371 0.371 c\n" +
198-
"-0.575 0.371 -0.741 0.204 -0.741 0 c\n" +
199-
"-0.741 -0.204 -0.575 -0.371 -0.371 -0.371 c\n" +
200-
"-0.166 -0.371 0 -0.204 0 0 c\n" +
201-
"f\n";
202-
203-
204-
protected static final String cross = "1 0 0 1 0.80 0.8 cm 0 0 m\n" +
205-
"-0.172 -0.027 l\n" +
206-
"-0.332 -0.184 l\n" +
207-
"-0.443 -0.019 l\n" +
208-
"-0.475 -0.009 l\n" +
209-
"-0.568 -0.168 l\n" +
210-
"-0.453 -0.324 l\n" +
211-
"-0.58 -0.497 l\n" +
212-
"-0.59 -0.641 l\n" +
213-
"-0.549 -0.627 l\n" +
214-
"-0.543 -0.612 -0.457 -0.519 -0.365 -0.419 c\n" +
215-
"-0.163 -0.572 l\n" +
216-
"-0.011 -0.536 l\n" +
217-
"-0.004 -0.507 l\n" +
218-
"-0.117 -0.441 l\n" +
219-
"-0.246 -0.294 l\n" +
220-
"-0.132 -0.181 l\n" +
221-
"0.031 -0.04 l\n" +
222-
"h\n" +
223-
"f\n";
224-
225-
protected static final String diamond = "1 0 0 1 0.5 0.12 cm 0 0 m\n" +
226-
"0.376 0.376 l\n" +
227-
"0 0.751 l\n" +
228-
"-0.376 0.376 l\n" +
229-
"h\n" +
230-
"f\n";
231-
232-
protected static final String square = "1 0 0 1 0.835 0.835 cm 0 0 -0.669 -0.67 re\n" +
233-
"f\n";
234-
235-
protected static final String star = "0.95 0 0 0.95 0.85 0.6 cm 0 0 m\n" +
236-
"-0.291 0 l\n" +
237-
"-0.381 0.277 l\n" +
238-
"-0.47 0 l\n" +
239-
"-0.761 0 l\n" +
240-
"-0.526 -0.171 l\n" +
241-
"-0.616 -0.448 l\n" +
242-
"-0.381 -0.277 l\n" +
243-
"-0.145 -0.448 l\n" +
244-
"-0.236 -0.171 l\n" +
245-
"h\n" +
246-
"f\n";
247-
248185
/**
249186
* Creates a form field as a wrapper object around a {@link PdfDictionary}.
250187
* This {@link PdfDictionary} must be an indirect object.
@@ -3059,10 +2996,8 @@ protected void drawPdfA1RadioAppearance(float width, float height, String value)
30592996
protected void drawRadioField(PdfCanvas canvas, float width, float height, boolean on) {
30602997
canvas.saveState();
30612998
if (on) {
3062-
canvas.
3063-
resetFillColorRgb().
3064-
circle(width / 2, height / 2, Math.min(width, height) / 4).
3065-
fill();
2999+
canvas.resetFillColorRgb();
3000+
DrawingUtil.drawCircle(canvas, width / 2, height / 2, Math.min(width, height) / 4);
30663001
}
30673002
canvas.restoreState();
30683003
}
@@ -3235,13 +3170,7 @@ protected void drawCheckBox(PdfCanvas canvas, float width, float height, float f
32353170
}
32363171

32373172
if (checkType == TYPE_CROSS) {
3238-
float offset = borderWidth * 2;
3239-
canvas.
3240-
moveTo((width - height) / 2 + offset, height - offset).
3241-
lineTo((width + height) / 2 - offset, offset).
3242-
moveTo((width + height) / 2 - offset, height - offset).
3243-
lineTo((width - height) / 2 + offset, offset).
3244-
stroke();
3173+
DrawingUtil.drawCross(canvas, width, height, borderWidth);
32453174
return;
32463175
}
32473176
PdfFont ufont = getFont();
@@ -3256,34 +3185,29 @@ protected void drawCheckBox(PdfCanvas canvas, float width, float height, float f
32563185
}
32573186

32583187
protected void drawPdfACheckBox(PdfCanvas canvas, float width, float height, boolean on) {
3259-
if (!on)
3188+
if (!on) {
32603189
return;
3261-
String appearanceString = check;
3190+
}
32623191
switch (checkType) {
32633192
case TYPE_CHECK:
3264-
appearanceString = check;
3193+
DrawingUtil.drawPdfACheck(canvas, width, height);
32653194
break;
32663195
case TYPE_CIRCLE:
3267-
appearanceString = circle;
3196+
DrawingUtil.drawPdfACircle(canvas, width, height);
32683197
break;
32693198
case TYPE_CROSS:
3270-
appearanceString = cross;
3199+
DrawingUtil.drawPdfACross(canvas, width, height);
32713200
break;
32723201
case TYPE_DIAMOND:
3273-
appearanceString = diamond;
3202+
DrawingUtil.drawPdfADiamond(canvas, width, height);
32743203
break;
32753204
case TYPE_SQUARE:
3276-
appearanceString = square;
3205+
DrawingUtil.drawPdfASquare(canvas, width, height);
32773206
break;
32783207
case TYPE_STAR:
3279-
appearanceString = star;
3208+
DrawingUtil.drawPdfAStar(canvas, width, height);
32803209
break;
32813210
}
3282-
canvas.saveState();
3283-
canvas.resetFillColorRgb();
3284-
canvas.concatMatrix(width, 0, 0, height, 0, 0);
3285-
canvas.getContentStream().getOutputStream().writeBytes(appearanceString.getBytes(StandardCharsets.ISO_8859_1));
3286-
canvas.restoreState();
32873211
}
32883212

32893213
private TextAlignment convertJustificationToTextAlignment() {
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2018 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
package com.itextpdf.forms.util;
45+
46+
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
47+
48+
import java.nio.charset.StandardCharsets;
49+
50+
/**
51+
* Utility class to draw form fields {@link com.itextpdf.forms.fields.PdfFormField}.
52+
*/
53+
public class DrawingUtil {
54+
protected static final String check = "0.8 0 0 0.8 0.3 0.5 cm 0 0 m\n" +
55+
"0.066 -0.026 l\n" +
56+
"0.137 -0.15 l\n" +
57+
"0.259 0.081 0.46 0.391 0.553 0.461 c\n" +
58+
"0.604 0.489 l\n" +
59+
"0.703 0.492 l\n" +
60+
"0.543 0.312 0.255 -0.205 0.154 -0.439 c\n" +
61+
"0.069 -0.399 l\n" +
62+
"0.035 -0.293 -0.039 -0.136 -0.091 -0.057 c\n" +
63+
"h\n" +
64+
"f\n";
65+
66+
protected static final String circle = "1 0 0 1 0.86 0.5 cm 0 0 m\n" +
67+
"0 0.204 -0.166 0.371 -0.371 0.371 c\n" +
68+
"-0.575 0.371 -0.741 0.204 -0.741 0 c\n" +
69+
"-0.741 -0.204 -0.575 -0.371 -0.371 -0.371 c\n" +
70+
"-0.166 -0.371 0 -0.204 0 0 c\n" +
71+
"f\n";
72+
73+
74+
protected static final String cross = "1 0 0 1 0.80 0.8 cm 0 0 m\n" +
75+
"-0.172 -0.027 l\n" +
76+
"-0.332 -0.184 l\n" +
77+
"-0.443 -0.019 l\n" +
78+
"-0.475 -0.009 l\n" +
79+
"-0.568 -0.168 l\n" +
80+
"-0.453 -0.324 l\n" +
81+
"-0.58 -0.497 l\n" +
82+
"-0.59 -0.641 l\n" +
83+
"-0.549 -0.627 l\n" +
84+
"-0.543 -0.612 -0.457 -0.519 -0.365 -0.419 c\n" +
85+
"-0.163 -0.572 l\n" +
86+
"-0.011 -0.536 l\n" +
87+
"-0.004 -0.507 l\n" +
88+
"-0.117 -0.441 l\n" +
89+
"-0.246 -0.294 l\n" +
90+
"-0.132 -0.181 l\n" +
91+
"0.031 -0.04 l\n" +
92+
"h\n" +
93+
"f\n";
94+
95+
protected static final String diamond = "1 0 0 1 0.5 0.12 cm 0 0 m\n" +
96+
"0.376 0.376 l\n" +
97+
"0 0.751 l\n" +
98+
"-0.376 0.376 l\n" +
99+
"h\n" +
100+
"f\n";
101+
102+
protected static final String square = "1 0 0 1 0.835 0.835 cm 0 0 -0.669 -0.67 re\n" +
103+
"f\n";
104+
105+
protected static final String star = "0.95 0 0 0.95 0.85 0.6 cm 0 0 m\n" +
106+
"-0.291 0 l\n" +
107+
"-0.381 0.277 l\n" +
108+
"-0.47 0 l\n" +
109+
"-0.761 0 l\n" +
110+
"-0.526 -0.171 l\n" +
111+
"-0.616 -0.448 l\n" +
112+
"-0.381 -0.277 l\n" +
113+
"-0.145 -0.448 l\n" +
114+
"-0.236 -0.171 l\n" +
115+
"h\n" +
116+
"f\n";
117+
118+
private static void drawPdfAAppearanceString(PdfCanvas canvas, float width, float height, float moveX, float moveY, String appearanceString) {
119+
canvas.saveState();
120+
canvas.resetFillColorRgb();
121+
canvas.concatMatrix(width, 0, 0, height, moveX, moveY);
122+
canvas.getContentStream().getOutputStream().writeBytes(appearanceString.getBytes(StandardCharsets.ISO_8859_1));
123+
canvas.restoreState();
124+
}
125+
126+
public static void drawPdfACheck(PdfCanvas canvas, float width, float height, float moveX, float moveY) {
127+
drawPdfAAppearanceString(canvas, width, height, moveX, moveY, check);
128+
}
129+
130+
public static void drawPdfACheck(PdfCanvas canvas, float width, float height) {
131+
drawPdfAAppearanceString(canvas, width, height, 0, 0, check);
132+
}
133+
134+
public static void drawPdfACircle(PdfCanvas canvas, float width, float height, float moveX, float moveY) {
135+
drawPdfAAppearanceString(canvas, width, height, moveX, moveY, circle);
136+
}
137+
138+
public static void drawPdfACircle(PdfCanvas canvas, float width, float height) {
139+
drawPdfAAppearanceString(canvas, width, height, 0, 0, circle);
140+
}
141+
142+
public static void drawPdfACross(PdfCanvas canvas, float width, float height, float moveX, float moveY) {
143+
drawPdfAAppearanceString(canvas, width, height, moveX, moveY, cross);
144+
}
145+
146+
public static void drawPdfACross(PdfCanvas canvas, float width, float height) {
147+
drawPdfAAppearanceString(canvas, width, height, 0, 0, cross);
148+
}
149+
150+
public static void drawPdfADiamond(PdfCanvas canvas, float width, float height, float moveX, float moveY) {
151+
drawPdfAAppearanceString(canvas, width, height, moveX, moveY, diamond);
152+
}
153+
154+
public static void drawPdfADiamond(PdfCanvas canvas, float width, float height) {
155+
drawPdfAAppearanceString(canvas, width, height, 0, 0, diamond);
156+
}
157+
158+
public static void drawPdfASquare(PdfCanvas canvas, float width, float height, float moveX, float moveY) {
159+
drawPdfAAppearanceString(canvas, width, height, moveX, moveY, square);
160+
}
161+
162+
public static void drawPdfASquare(PdfCanvas canvas, float width, float height) {
163+
drawPdfAAppearanceString(canvas, width, height, 0, 0, square);
164+
}
165+
166+
public static void drawPdfAStar(PdfCanvas canvas, float width, float height, float moveX, float moveY) {
167+
drawPdfAAppearanceString(canvas, width, height, moveX, moveY, star);
168+
}
169+
170+
public static void drawPdfAStar(PdfCanvas canvas, float width, float height) {
171+
drawPdfAAppearanceString(canvas, width, height, 0, 0, star);
172+
}
173+
174+
public static void drawCross(PdfCanvas canvas, float width, float height, float borderWidth) {
175+
float offset = borderWidth * 2;
176+
canvas.
177+
moveTo((width - height) / 2 + offset, height - offset).
178+
lineTo((width + height) / 2 - offset, offset).
179+
moveTo((width + height) / 2 - offset, height - offset).
180+
lineTo((width - height) / 2 + offset, offset).
181+
stroke();
182+
}
183+
184+
public static void drawCircle(PdfCanvas canvas, float centerX, float centerY, float radius) {
185+
canvas
186+
.circle(centerX, centerY, radius)
187+
.fill();
188+
}
189+
}

0 commit comments

Comments
 (0)