Skip to content

Commit 9761e39

Browse files
author
Vitali Prudnikovich
committed
Allow putting signatures into existing field deep in the hierarchy
DEVSIX-3717
1 parent 3b69a89 commit 9761e39

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

sign/src/main/java/com/itextpdf/signatures/PdfSigner.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,10 @@ public String getNewSigFieldName() {
381381
*/
382382
public void setFieldName(String fieldName) {
383383
if (fieldName != null) {
384-
if (fieldName.indexOf('.') >= 0) {
385-
throw new IllegalArgumentException(SignExceptionMessageConstant.FIELD_NAMES_CANNOT_CONTAIN_A_DOT);
386-
}
387-
388384
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(document, true);
389385

390-
if (acroForm.getField(fieldName) != null) {
391-
PdfFormField field = acroForm.getField(fieldName);
392-
386+
PdfFormField field = acroForm.getField(fieldName);
387+
if (field != null) {
393388
if (!PdfName.Sig.equals(field.getFormType())) {
394389
throw new IllegalArgumentException(
395390
SignExceptionMessageConstant.FIELD_TYPE_IS_NOT_A_SIGNATURE_FIELD_TYPE);
@@ -407,6 +402,12 @@ public void setFieldName(String fieldName) {
407402
appearance.setPageRect(getWidgetRectangle(widget));
408403
appearance.setPageNumber(getWidgetPageNumber(widget));
409404
}
405+
} else {
406+
// Do not allow dots for new fields
407+
// For existing fields dots are allowed because there it might be fully qualified name
408+
if (fieldName.indexOf('.') >= 0) {
409+
throw new IllegalArgumentException(SignExceptionMessageConstant.FIELD_NAMES_CANNOT_CONTAIN_A_DOT);
410+
}
410411
}
411412

412413
this.fieldName = fieldName;

sign/src/test/java/com/itextpdf/signatures/sign/SimpleSigningTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ public void signWithoutPKeyTest() throws GeneralSecurityException, IOException,
122122
Assert.assertNull(SignaturesCompareTool.compareSignatures(outPdf, cmpPdf));
123123
}
124124

125+
@Test
126+
public void signIntoExistingFieldWithDotsTest() throws GeneralSecurityException, IOException, InterruptedException {
127+
String srcFile = SOURCE_FOLDER + "signIntoExistingFieldWithDots.pdf";
128+
String cmpPdf = SOURCE_FOLDER + "cmp_signIntoExistingFieldWithDots.pdf";
129+
String outPdf = DESTINATION_FOLDER + "signIntoExistingFieldWithDots.pdf";
130+
131+
Rectangle randomRect = new Rectangle(1, 1, 100, 100);
132+
String fieldName = "Signature1.1";
133+
sign(srcFile, fieldName, outPdf, chain, pk, DigestAlgorithms.SHA256, PdfSigner.CryptoStandard.CADES, "Test 1",
134+
"TestCity", randomRect, false, false, PdfSigner.NOT_CERTIFIED, 12f);
135+
136+
Assert.assertNull(new CompareTool().compareVisually(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_",
137+
getTestMap(new Rectangle(163, 128, 430, 202))));
138+
139+
Assert.assertNull(SignaturesCompareTool.compareSignatures(outPdf, cmpPdf));
140+
}
141+
125142
@Test
126143
public void signWithTempFileTest() throws GeneralSecurityException, IOException, InterruptedException {
127144
String srcFile = SOURCE_FOLDER + "simpleDocument.pdf";

0 commit comments

Comments
 (0)