|
| 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