|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2018 iText Group NV |
| 4 | + Authors: iText Software. |
| 5 | +
|
| 6 | + This program is free software; you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU Affero General Public License version 3 |
| 8 | + as published by the Free Software Foundation with the addition of the |
| 9 | + following permission added to Section 15 as permitted in Section 7(a): |
| 10 | + FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY |
| 11 | + ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT |
| 12 | + OF THIRD PARTY RIGHTS |
| 13 | +
|
| 14 | + This program is distributed in the hope that it will be useful, but |
| 15 | + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | + or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | + See the GNU Affero General Public License for more details. |
| 18 | + You should have received a copy of the GNU Affero General Public License |
| 19 | + along with this program; if not, see http://www.gnu.org/licenses or write to |
| 20 | + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | + Boston, MA, 02110-1301 USA, or download the license from the following URL: |
| 22 | + http://itextpdf.com/terms-of-use/ |
| 23 | +
|
| 24 | + The interactive user interfaces in modified source and object code versions |
| 25 | + of this program must display Appropriate Legal Notices, as required under |
| 26 | + Section 5 of the GNU Affero General Public License. |
| 27 | +
|
| 28 | + In accordance with Section 7(b) of the GNU Affero General Public License, |
| 29 | + a covered work must retain the producer line in every PDF that is created |
| 30 | + or manipulated using iText. |
| 31 | +
|
| 32 | + You can be released from the requirements of the license by purchasing |
| 33 | + a commercial license. Buying such a license is mandatory as soon as you |
| 34 | + develop commercial activities involving the iText software without |
| 35 | + disclosing the source code of your own applications. |
| 36 | + These activities include: offering paid services to customers as an ASP, |
| 37 | + serving PDFs on the fly in a web application, shipping iText with a closed |
| 38 | + source product. |
| 39 | +
|
| 40 | + For more information, please contact iText Software Corp. at this |
| 41 | + |
| 42 | + */ |
| 43 | +package com.itextpdf.pdfa; |
| 44 | + |
| 45 | +import com.itextpdf.forms.PdfAcroForm; |
| 46 | +import com.itextpdf.forms.fields.PdfButtonFormField; |
| 47 | +import com.itextpdf.forms.fields.PdfFormField; |
| 48 | +import com.itextpdf.kernel.colors.ColorConstants; |
| 49 | +import com.itextpdf.kernel.font.PdfFont; |
| 50 | +import com.itextpdf.kernel.font.PdfFontFactory; |
| 51 | +import com.itextpdf.kernel.geom.PageSize; |
| 52 | +import com.itextpdf.kernel.geom.Rectangle; |
| 53 | +import com.itextpdf.kernel.pdf.PdfAConformanceLevel; |
| 54 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 55 | +import com.itextpdf.kernel.pdf.PdfName; |
| 56 | +import com.itextpdf.kernel.pdf.PdfOutputIntent; |
| 57 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 58 | +import com.itextpdf.kernel.pdf.canvas.PdfCanvas; |
| 59 | +import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; |
| 60 | +import com.itextpdf.kernel.utils.CompareTool; |
| 61 | +import com.itextpdf.layout.Document; |
| 62 | +import com.itextpdf.layout.element.Image; |
| 63 | +import com.itextpdf.layout.element.Paragraph; |
| 64 | +import com.itextpdf.layout.element.Text; |
| 65 | +import com.itextpdf.layout.renderer.DrawContext; |
| 66 | +import com.itextpdf.layout.renderer.ParagraphRenderer; |
| 67 | +import com.itextpdf.test.ExtendedITextTest; |
| 68 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 69 | +import org.junit.Assert; |
| 70 | +import org.junit.BeforeClass; |
| 71 | +import org.junit.Test; |
| 72 | +import org.junit.experimental.categories.Category; |
| 73 | + |
| 74 | +import java.io.FileInputStream; |
| 75 | +import java.io.FileOutputStream; |
| 76 | +import java.io.InputStream; |
| 77 | + |
| 78 | +@Category(IntegrationTest.class) |
| 79 | +public class PdfAFormFieldTest extends ExtendedITextTest { |
| 80 | + |
| 81 | + public static final String sourceFolder = "./src/test/resources/com/itextpdf/pdfa/"; |
| 82 | + public static final String destinationFolder = "./target/test/com/itextpdf/pdfa/PdfAFormFieldTest/"; |
| 83 | + |
| 84 | + @BeforeClass |
| 85 | + public static void beforeClass() { |
| 86 | + createDestinationFolder(destinationFolder); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void pdfAButtonFieldTest() throws Exception { |
| 91 | + PdfDocument pdf; |
| 92 | + InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm"); |
| 93 | + String file = "pdfAButtonField.pdf"; |
| 94 | + String filename = destinationFolder + file; |
| 95 | + pdf = new PdfADocument( |
| 96 | + new PdfWriter(new FileOutputStream(filename)), |
| 97 | + PdfAConformanceLevel.PDF_A_1B, |
| 98 | + new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB ICC preference", is)); |
| 99 | + |
| 100 | + PageSize pageSize = PageSize.LETTER; |
| 101 | + Document doc = new Document(pdf, pageSize); |
| 102 | + PdfFontFactory.register(sourceFolder + "FreeSans.ttf",sourceFolder + "FreeSans.ttf"); |
| 103 | + PdfFont font = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf", true); |
| 104 | + |
| 105 | + PdfButtonFormField group = PdfFormField.createRadioGroup(pdf, "group", "", PdfAConformanceLevel.PDF_A_1B); |
| 106 | + group.setReadOnly(true); |
| 107 | + |
| 108 | + Paragraph p = new Paragraph(); |
| 109 | + Text t = new Text("supported"); |
| 110 | + |
| 111 | + t.setFont(font); |
| 112 | + |
| 113 | + p.add(t); |
| 114 | + |
| 115 | + Image ph = new Image(new PdfFormXObject(new Rectangle(10, 10))); |
| 116 | + Paragraph pc = new Paragraph().add(ph); |
| 117 | + PdfAButtonFieldTestRenderer r = new PdfAButtonFieldTestRenderer(pc, group, "v1"); |
| 118 | + |
| 119 | + pc.setNextRenderer(r); |
| 120 | + |
| 121 | + p.add(pc); |
| 122 | + |
| 123 | + Paragraph pc1 = new Paragraph().add(ph); |
| 124 | + PdfAButtonFieldTestRenderer r1 = new PdfAButtonFieldTestRenderer(pc, group, "v2"); |
| 125 | + pc1.setNextRenderer(r1); |
| 126 | + |
| 127 | + Paragraph p2 = new Paragraph(); |
| 128 | + Text t2 = new Text("supported 2"); |
| 129 | + t2.setFont(font); |
| 130 | + |
| 131 | + p2.add(t2).add(pc1); |
| 132 | + |
| 133 | + doc.add(p); |
| 134 | + doc.add(p2); |
| 135 | + group.setValue("v1"); |
| 136 | + |
| 137 | + pdf.close(); |
| 138 | + Assert.assertNull(new CompareTool().compareByContent(filename, sourceFolder + "cmp_" + file, destinationFolder, "diff_")); |
| 139 | + } |
| 140 | + |
| 141 | + static class PdfAButtonFieldTestRenderer extends ParagraphRenderer { |
| 142 | + private PdfButtonFormField _group; |
| 143 | + private String _value; |
| 144 | + |
| 145 | + public PdfAButtonFieldTestRenderer(Paragraph para, PdfButtonFormField group, String value) |
| 146 | + { |
| 147 | + super(para); |
| 148 | + _group = group; |
| 149 | + _value = value; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void draw(DrawContext context) |
| 154 | + { |
| 155 | + int pageNumber = getOccupiedArea().getPageNumber(); |
| 156 | + Rectangle bbox = getInnerAreaBBox(); |
| 157 | + PdfDocument pdf = context.getDocument(); |
| 158 | + PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true); |
| 159 | + PdfFormField chk = PdfFormField.createRadioButton(pdf, bbox, _group, _value, PdfAConformanceLevel.PDF_A_1B); |
| 160 | + chk.setPage(pageNumber); |
| 161 | + chk.setValue("Off"); |
| 162 | + chk.regenerateField(); |
| 163 | + |
| 164 | + chk.setVisibility(PdfFormField.VISIBLE); |
| 165 | + chk.setBorderColor(ColorConstants.BLACK); |
| 166 | + chk.setBackgroundColor(ColorConstants.WHITE); |
| 167 | + chk.setReadOnly(true); |
| 168 | + |
| 169 | + PdfFormXObject appearance = new PdfFormXObject(bbox); |
| 170 | + PdfCanvas canvas = new PdfCanvas(appearance, pdf); |
| 171 | + |
| 172 | + canvas.saveState() |
| 173 | + .moveTo(bbox.getLeft(), bbox.getBottom()) |
| 174 | + .lineTo(bbox.getRight(), bbox.getBottom()) |
| 175 | + .lineTo(bbox.getRight(), bbox.getTop()) |
| 176 | + .lineTo(bbox.getLeft(), bbox.getTop()) |
| 177 | + .lineTo(bbox.getLeft(), bbox.getBottom()) |
| 178 | + .setLineWidth(1f) |
| 179 | + .stroke() |
| 180 | + .restoreState(); |
| 181 | + |
| 182 | + form.addFieldAppearanceToPage(chk, pdf.getPage(pageNumber)); |
| 183 | + chk.setAppearance(PdfName.N, "v1".equals(_value) ? _value : "Off", appearance.getPdfObject()); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | + |
0 commit comments