|
| 1 | +package com.itextpdf.signatures; |
| 2 | + |
| 3 | +import com.itextpdf.forms.PdfAcroForm; |
| 4 | +import com.itextpdf.forms.PdfSigFieldLock; |
| 5 | +import com.itextpdf.forms.fields.PdfFormField; |
| 6 | +import com.itextpdf.forms.fields.PdfSignatureFormField; |
| 7 | +import com.itextpdf.io.source.ByteArrayOutputStream; |
| 8 | +import com.itextpdf.kernel.geom.Rectangle; |
| 9 | +import com.itextpdf.kernel.pdf.EncryptionConstants; |
| 10 | +import com.itextpdf.kernel.pdf.PdfDictionary; |
| 11 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 12 | +import com.itextpdf.kernel.pdf.PdfName; |
| 13 | +import com.itextpdf.kernel.pdf.PdfReader; |
| 14 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 15 | +import com.itextpdf.kernel.pdf.ReaderProperties; |
| 16 | +import com.itextpdf.kernel.pdf.StampingProperties; |
| 17 | +import com.itextpdf.kernel.pdf.WriterProperties; |
| 18 | +import com.itextpdf.kernel.pdf.annot.PdfWidgetAnnotation; |
| 19 | +import com.itextpdf.test.ExtendedITextTest; |
| 20 | +import com.itextpdf.test.annotations.type.UnitTest; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.experimental.categories.Category; |
| 24 | + |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.IOException; |
| 27 | + |
| 28 | +@Category(UnitTest.class) |
| 29 | +public class PdfSignerUnitTest extends ExtendedITextTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void createNewSignatureFormFieldInvisibleAnnotationTest() throws IOException { |
| 33 | + PdfSigner signer = new PdfSigner( |
| 34 | + new PdfReader(new ByteArrayInputStream(createDocumentWithoutWidgetAnnotation()), |
| 35 | + new ReaderProperties().setPassword("owner".getBytes())), new ByteArrayOutputStream(), new StampingProperties()); |
| 36 | + signer.cryptoDictionary = new PdfSignature(); |
| 37 | + signer.appearance.setPageRect(new Rectangle(100, 100, 0, 0)); |
| 38 | + |
| 39 | + PdfAcroForm acroForm = PdfAcroForm.getAcroForm(signer.document, true); |
| 40 | + signer.createNewSignatureFormField(acroForm, signer.fieldName); |
| 41 | + PdfFormField formField = acroForm.getField(signer.fieldName); |
| 42 | + |
| 43 | + PdfDictionary formFieldDictionary = formField.getPdfObject(); |
| 44 | + Assert.assertNotNull(formFieldDictionary); |
| 45 | + Assert.assertFalse(formFieldDictionary.containsKey(PdfName.AP)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void createNewSignatureFormFieldNotInvisibleAnnotationTest() throws IOException { |
| 50 | + PdfSigner signer = new PdfSigner( |
| 51 | + new PdfReader(new ByteArrayInputStream(createDocumentWithoutWidgetAnnotation()), |
| 52 | + new ReaderProperties().setPassword("owner".getBytes())), new ByteArrayOutputStream(), new StampingProperties()); |
| 53 | + signer.cryptoDictionary = new PdfSignature(); |
| 54 | + signer.appearance.setPageRect(new Rectangle(100, 100, 10, 10)); |
| 55 | + PdfSigFieldLock fieldLock = new PdfSigFieldLock(); |
| 56 | + signer.fieldLock = fieldLock; |
| 57 | + |
| 58 | + PdfAcroForm acroForm = PdfAcroForm.getAcroForm(signer.document, true); |
| 59 | + Assert.assertEquals(fieldLock, signer.createNewSignatureFormField(acroForm, signer.fieldName)); |
| 60 | + PdfFormField formField = acroForm.getField(signer.fieldName); |
| 61 | + |
| 62 | + PdfDictionary formFieldDictionary = formField.getPdfObject(); |
| 63 | + Assert.assertNotNull(formFieldDictionary); |
| 64 | + Assert.assertTrue(formFieldDictionary.containsKey(PdfName.AP)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void populateExistingSignatureFormFieldInvisibleAnnotationTest() throws IOException { |
| 69 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 70 | + PdfDocument document = new PdfDocument(new PdfWriter(outputStream, |
| 71 | + new WriterProperties().setStandardEncryption("user".getBytes(), "owner".getBytes(), 0, EncryptionConstants.STANDARD_ENCRYPTION_128))); |
| 72 | + document.addNewPage(); |
| 73 | + PdfWidgetAnnotation widgetAnnotation = new PdfWidgetAnnotation(new Rectangle(100, 100, 0, 0)); |
| 74 | + document.getPage(1).addAnnotation(widgetAnnotation); |
| 75 | + document.close(); |
| 76 | + PdfSigner signer = new PdfSigner( |
| 77 | + new PdfReader(new ByteArrayInputStream(outputStream.toByteArray()), new ReaderProperties().setPassword("owner".getBytes())), |
| 78 | + new ByteArrayOutputStream(), new StampingProperties()); |
| 79 | + signer.cryptoDictionary = new PdfSignature(); |
| 80 | + signer.appearance.setPageRect(new Rectangle(100, 100, 0, 0)); |
| 81 | + |
| 82 | + widgetAnnotation = (PdfWidgetAnnotation) signer.document.getPage(1).getAnnotations().get(0); |
| 83 | + PdfAcroForm acroForm = PdfAcroForm.getAcroForm(signer.document, true); |
| 84 | + PdfFormField formField = new ExtendedPdfSignatureFormField(widgetAnnotation, signer.document); |
| 85 | + formField.setFieldName(signer.fieldName); |
| 86 | + acroForm.addField(formField); |
| 87 | + signer.populateExistingSignatureFormField(acroForm); |
| 88 | + formField = acroForm.getField(signer.fieldName); |
| 89 | + |
| 90 | + PdfDictionary formFieldDictionary = formField.getPdfObject(); |
| 91 | + Assert.assertNotNull(formFieldDictionary); |
| 92 | + Assert.assertFalse(formFieldDictionary.containsKey(PdfName.AP)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void populateExistingSignatureFormFieldNotInvisibleAnnotationTest() throws IOException { |
| 97 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 98 | + PdfDocument document = new PdfDocument(new PdfWriter(outputStream, |
| 99 | + new WriterProperties().setStandardEncryption("user".getBytes(), "owner".getBytes(), 0, EncryptionConstants.STANDARD_ENCRYPTION_128))); |
| 100 | + document.addNewPage(); |
| 101 | + PdfWidgetAnnotation widgetAnnotation = new PdfWidgetAnnotation(new Rectangle(100, 100, 0, 0)); |
| 102 | + document.getPage(1).addAnnotation(widgetAnnotation); |
| 103 | + document.close(); |
| 104 | + PdfSigner signer = new PdfSigner( |
| 105 | + new PdfReader(new ByteArrayInputStream(outputStream.toByteArray()), new ReaderProperties().setPassword("owner".getBytes())), |
| 106 | + new ByteArrayOutputStream(), new StampingProperties()); |
| 107 | + signer.cryptoDictionary = new PdfSignature(); |
| 108 | + PdfSigFieldLock fieldLock = new PdfSigFieldLock(); |
| 109 | + signer.fieldLock = fieldLock; |
| 110 | + signer.appearance.setPageRect(new Rectangle(100, 100, 10, 10)); |
| 111 | + |
| 112 | + widgetAnnotation = (PdfWidgetAnnotation) signer.document.getPage(1).getAnnotations().get(0); |
| 113 | + PdfAcroForm acroForm = PdfAcroForm.getAcroForm(signer.document, true); |
| 114 | + PdfFormField formField = new ExtendedPdfSignatureFormField(widgetAnnotation, signer.document); |
| 115 | + formField.setFieldName(signer.fieldName); |
| 116 | + acroForm.addField(formField); |
| 117 | + Assert.assertEquals(signer.populateExistingSignatureFormField(acroForm), fieldLock); |
| 118 | + formField = acroForm.getField(signer.fieldName); |
| 119 | + |
| 120 | + PdfDictionary formFieldDictionary = formField.getPdfObject(); |
| 121 | + Assert.assertNotNull(formFieldDictionary); |
| 122 | + Assert.assertTrue(formFieldDictionary.containsKey(PdfName.AP)); |
| 123 | + } |
| 124 | + |
| 125 | + private static byte[] createDocumentWithoutWidgetAnnotation() { |
| 126 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 127 | + PdfDocument document = new PdfDocument(new PdfWriter(outputStream, |
| 128 | + new WriterProperties().setStandardEncryption("user".getBytes(), "owner".getBytes(), 0, EncryptionConstants.STANDARD_ENCRYPTION_128))); |
| 129 | + document.addNewPage(); |
| 130 | + document.close(); |
| 131 | + return outputStream.toByteArray(); |
| 132 | + } |
| 133 | + |
| 134 | + static class ExtendedPdfSignatureFormField extends PdfSignatureFormField { |
| 135 | + public ExtendedPdfSignatureFormField(PdfWidgetAnnotation widgetAnnotation, PdfDocument document) { |
| 136 | + super(widgetAnnotation, document); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments