|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2023 Apryse Group NV |
| 4 | + Authors: Apryse Software. |
| 5 | +
|
| 6 | + This program is offered under a commercial and under the AGPL license. |
| 7 | + For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | + AGPL licensing: |
| 10 | + This program is free software: you can redistribute it and/or modify |
| 11 | + it under the terms of the GNU Affero General Public License as published by |
| 12 | + the Free Software Foundation, either version 3 of the License, or |
| 13 | + (at your option) any later version. |
| 14 | +
|
| 15 | + This program is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU Affero General Public License |
| 21 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +package com.itextpdf.forms; |
| 24 | + |
| 25 | +import com.itextpdf.forms.fields.PdfFormCreator; |
| 26 | +import com.itextpdf.forms.fields.PdfTextFormField; |
| 27 | +import com.itextpdf.forms.fields.TextFormFieldBuilder; |
| 28 | +import com.itextpdf.forms.form.FormProperty; |
| 29 | +import com.itextpdf.forms.form.element.InputField; |
| 30 | +import com.itextpdf.forms.form.element.SignatureFieldAppearance; |
| 31 | +import com.itextpdf.forms.form.element.TextArea; |
| 32 | +import com.itextpdf.kernel.colors.ColorConstants; |
| 33 | +import com.itextpdf.kernel.font.PdfFont; |
| 34 | +import com.itextpdf.kernel.font.PdfFontFactory; |
| 35 | +import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy; |
| 36 | +import com.itextpdf.kernel.geom.Rectangle; |
| 37 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 38 | +import com.itextpdf.kernel.pdf.PdfDocumentInfo; |
| 39 | +import com.itextpdf.kernel.pdf.PdfName; |
| 40 | +import com.itextpdf.kernel.pdf.PdfString; |
| 41 | +import com.itextpdf.kernel.pdf.PdfVersion; |
| 42 | +import com.itextpdf.kernel.pdf.PdfViewerPreferences; |
| 43 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 44 | +import com.itextpdf.kernel.pdf.WriterProperties; |
| 45 | +import com.itextpdf.kernel.pdf.tagging.StandardRoles; |
| 46 | +import com.itextpdf.kernel.pdf.tagutils.TagTreePointer; |
| 47 | +import com.itextpdf.kernel.utils.CompareTool; |
| 48 | +import com.itextpdf.kernel.xmp.XMPException; |
| 49 | +import com.itextpdf.kernel.xmp.XMPMeta; |
| 50 | +import com.itextpdf.kernel.xmp.XMPMetaFactory; |
| 51 | +import com.itextpdf.layout.Document; |
| 52 | +import com.itextpdf.layout.borders.SolidBorder; |
| 53 | +import com.itextpdf.test.ExtendedITextTest; |
| 54 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 55 | +import com.itextpdf.test.pdfa.VeraPdfValidator; // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android) |
| 56 | + |
| 57 | +import java.io.ByteArrayInputStream; |
| 58 | +import java.io.IOException; |
| 59 | +import java.nio.file.Files; |
| 60 | +import java.nio.file.Paths; |
| 61 | +import org.junit.Assert; |
| 62 | +import org.junit.BeforeClass; |
| 63 | +import org.junit.Test; |
| 64 | +import org.junit.experimental.categories.Category; |
| 65 | +import static org.junit.Assert.fail; |
| 66 | + |
| 67 | +@Category(IntegrationTest.class) |
| 68 | +public class PdfUA2FormTest extends ExtendedITextTest { |
| 69 | + public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/forms/PdfUA2FormTest/"; |
| 70 | + public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/forms//PdfUA2FormTest/"; |
| 71 | + |
| 72 | + @BeforeClass |
| 73 | + public static void beforeClass() { |
| 74 | + createOrClearDestinationFolder(DESTINATION_FOLDER); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void checkFormFieldTest() throws IOException, XMPException, InterruptedException { |
| 79 | + String outFile = DESTINATION_FOLDER + "formFieldTest.pdf"; |
| 80 | + String cmpFile = SOURCE_FOLDER + "cmp_formFieldTest.pdf"; |
| 81 | + |
| 82 | + try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion( |
| 83 | + PdfVersion.PDF_2_0)))){ |
| 84 | + Document document = new Document(pdfDocument); |
| 85 | + PdfFont font = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf", |
| 86 | + "WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED); |
| 87 | + document.setFont(font); |
| 88 | + createSimplePdfUA2Document(pdfDocument); |
| 89 | + |
| 90 | + PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDocument, true); |
| 91 | + Rectangle rect = new Rectangle(210, 490, 150, 22); |
| 92 | + PdfTextFormField field = new TextFormFieldBuilder(pdfDocument, "fieldName") |
| 93 | + .setWidgetRectangle(rect).createText(); |
| 94 | + field.put(PdfName.Contents, new PdfString("Description")); |
| 95 | + field.setValue("some value"); |
| 96 | + field.setFont(font); |
| 97 | + form.addField(field); |
| 98 | + } |
| 99 | + compareAndValidate(outFile, cmpFile); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void checkTextAreaTest() throws IOException, XMPException, InterruptedException { |
| 104 | + String outFile = DESTINATION_FOLDER + "textAreaTest.pdf"; |
| 105 | + String cmpFile = SOURCE_FOLDER + "cmp_textAreaTest.pdf"; |
| 106 | + |
| 107 | + try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion( |
| 108 | + PdfVersion.PDF_2_0)))){ |
| 109 | + Document document = new Document(pdfDocument); |
| 110 | + PdfFont font = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf", |
| 111 | + "WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED); |
| 112 | + document.setFont(font); |
| 113 | + createSimplePdfUA2Document(pdfDocument); |
| 114 | + |
| 115 | + TextArea formTextArea = new TextArea("form text area"); |
| 116 | + formTextArea.setProperty(FormProperty.FORM_FIELD_FLATTEN, false); |
| 117 | + formTextArea.setProperty(FormProperty.FORM_FIELD_VALUE, "form\ntext\narea"); |
| 118 | + |
| 119 | + document.add(formTextArea); |
| 120 | + PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDocument, true); |
| 121 | + form.getField("form text area").getPdfObject().put(PdfName.Contents, new PdfString("Description")); |
| 122 | + } |
| 123 | + compareAndValidate(outFile, cmpFile); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void checkInputFieldTest() throws IOException, XMPException, InterruptedException { |
| 128 | + String outFile = DESTINATION_FOLDER + "inputFieldTest.pdf"; |
| 129 | + String cmpFile = SOURCE_FOLDER + "cmp_inputFieldTest.pdf"; |
| 130 | + |
| 131 | + try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion( |
| 132 | + PdfVersion.PDF_2_0)))){ |
| 133 | + Document document = new Document(pdfDocument); |
| 134 | + PdfFont font = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf", |
| 135 | + "WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED); |
| 136 | + document.setFont(font); |
| 137 | + createSimplePdfUA2Document(pdfDocument); |
| 138 | + |
| 139 | + InputField formInputField = new InputField("form input field"); |
| 140 | + formInputField.setProperty(FormProperty.FORM_FIELD_FLATTEN, false); |
| 141 | + formInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "form input field"); |
| 142 | + formInputField.setProperty(FormProperty.FORM_FIELD_LABEL, "label form field"); |
| 143 | + |
| 144 | + document.add(formInputField); |
| 145 | + PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDocument, true); |
| 146 | + form.getField("form input field").getPdfObject().put(PdfName.Contents, new PdfString("Description")); |
| 147 | + } |
| 148 | + compareAndValidate(outFile, cmpFile); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void checkSignatureFormTest() throws IOException, XMPException, InterruptedException { |
| 153 | + String outFile = DESTINATION_FOLDER + "signatureFormTest.pdf"; |
| 154 | + String cmpFile = SOURCE_FOLDER + "cmp_signatureFormTest.pdf"; |
| 155 | + |
| 156 | + try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion( |
| 157 | + PdfVersion.PDF_2_0)))){ |
| 158 | + Document document = new Document(pdfDocument); |
| 159 | + PdfFont font = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf", |
| 160 | + "WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED); |
| 161 | + document.setFont(font); |
| 162 | + createSimplePdfUA2Document(pdfDocument); |
| 163 | + TagTreePointer tagPointer = pdfDocument.getTagStructureContext().getAutoTaggingPointer(); |
| 164 | + tagPointer.addTag(StandardRoles.FIGURE); |
| 165 | + tagPointer.getProperties().setAlternateDescription("Alt Description"); |
| 166 | + |
| 167 | + SignatureFieldAppearance formSigField = new SignatureFieldAppearance("form SigField"); |
| 168 | + formSigField.setProperty(FormProperty.FORM_FIELD_FLATTEN, false); |
| 169 | + formSigField.setContent("form SigField"); |
| 170 | + |
| 171 | + formSigField.setBorder(new SolidBorder(ColorConstants.YELLOW, 1)); |
| 172 | + formSigField.setFont(font); |
| 173 | + document.add(formSigField); |
| 174 | + PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDocument, true); |
| 175 | + form.getField("form SigField").getPdfObject().put(PdfName.Contents, new PdfString("Description")); |
| 176 | + } |
| 177 | + compareAndValidate(outFile, cmpFile); |
| 178 | + } |
| 179 | + |
| 180 | + private void createSimplePdfUA2Document(PdfDocument pdfDocument) throws IOException, XMPException { |
| 181 | + byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp")); |
| 182 | + XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes)); |
| 183 | + pdfDocument.setXmpMetadata(xmpMeta); |
| 184 | + pdfDocument.setTagged(); |
| 185 | + pdfDocument.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true)); |
| 186 | + pdfDocument.getCatalog().setLang(new PdfString("en-US")); |
| 187 | + PdfDocumentInfo info = pdfDocument.getDocumentInfo(); |
| 188 | + info.setTitle("PdfUA2 Title"); |
| 189 | + } |
| 190 | + |
| 191 | + private void compareAndValidate(String outPdf, String cmpPdf) throws IOException, InterruptedException { |
| 192 | + Assert.assertNull(new VeraPdfValidator().validate(outPdf)); // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android) |
| 193 | + String result = new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_"); |
| 194 | + if (result != null) { |
| 195 | + fail(result); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments