Skip to content

Commit 8b1b290

Browse files
author
Eugene Bochilo
committed
Finalize PdfPadesSigner API
DEVSIX-7800
1 parent 856d87d commit 8b1b290

19 files changed

+442
-125
lines changed

sharpenConfiguration.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@
481481
<file path="com/itextpdf/signatures/sign/LtvSigTest.java"/>
482482
<file path="com/itextpdf/signatures/sign/PdfPadesAdvancedTest.java"/>
483483
<file path="com/itextpdf/signatures/sign/PadesSignatureLevelTest.java"/>
484+
<file path="com/itextpdf/signatures/sign/PdfPadesSignerTest.java"/>
484485
<file path="com/itextpdf/signatures/sign/TimestampSigTest.java"/>
485486
</fileset>
486487
<fileset reason=".pem files reading logic is different in java and .net">
@@ -634,6 +635,8 @@
634635
<file path="com/itextpdf/signatures/sign/PdfPadesAdvancedTest/cmp_signedWith_signCertOcspNoCrl_rootCertCrlOcsp_revoked.pdf"/>
635636
<file path="com/itextpdf/signatures/sign/PdfPadesAdvancedTest/cmp_signedWith_signCertOcspNoCrl_rootCertOcspNoCrl_revoked.pdf"/>
636637
<file path="com/itextpdf/signatures/sign/PdfPadesAdvancedTest/cmp_signedWith_signCertOcspNoCrl_rootCertNoCrlNoOcsp_revoked.pdf"/>
638+
639+
<file path="com/itextpdf/signatures/sign/PdfPadesSignerTest/cmp_defaultSignerPropertiesTest.pdf"/>
637640

638641
<file path="com/itextpdf/signatures/sign/TimestampSigTest/cmp_timestampTest01.pdf"/>
639642
<file path="com/itextpdf/signatures/PKCS7ExternalSignatureContainerTest/cmp_testTroughPdfSignerWithTsaClient.pdf"/>

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

Lines changed: 86 additions & 52 deletions
Large diffs are not rendered by default.
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
package com.itextpdf.signatures;
2+
3+
import com.itextpdf.commons.utils.DateTimeUtil;
4+
import com.itextpdf.forms.PdfSigFieldLock;
5+
import com.itextpdf.forms.form.element.SignatureFieldAppearance;
6+
import com.itextpdf.kernel.geom.Rectangle;
7+
8+
import java.util.Calendar;
9+
10+
/**
11+
* Properties to be used in signing operations.
12+
*/
13+
public class SignerProperties {
14+
15+
private PdfSigFieldLock fieldLock;
16+
private SignatureFieldAppearance appearance;
17+
private Calendar signDate = DateTimeUtil.getCurrentTimeCalendar();
18+
private int certificationLevel = PdfSigner.NOT_CERTIFIED;
19+
private String fieldName;
20+
private int pageNumber = 1;
21+
private Rectangle pageRect = new Rectangle(0, 0);
22+
23+
private String signatureCreator = "";
24+
25+
private String contact = "";
26+
27+
/**
28+
* Create instance of {@link SignerProperties}.
29+
*/
30+
public SignerProperties() {
31+
// Empty constructor.
32+
}
33+
34+
/**
35+
* Gets the signature date.
36+
*
37+
* @return Calendar set to the signature date
38+
*/
39+
public java.util.Calendar getSignDate() {
40+
return signDate;
41+
}
42+
43+
/**
44+
* Sets the signature date.
45+
*
46+
* @param signDate the signature date
47+
*/
48+
public SignerProperties setSignDate(java.util.Calendar signDate) {
49+
this.signDate = signDate;
50+
return this;
51+
}
52+
53+
/**
54+
* Sets the signature field layout element to customize the appearance of the signature. Signer's sign date will
55+
* be set.
56+
*
57+
* @param appearance the {@link SignatureFieldAppearance} layout element.
58+
*/
59+
public SignerProperties setSignatureAppearance(SignatureFieldAppearance appearance) {
60+
this.appearance = appearance;
61+
return this;
62+
}
63+
64+
/**
65+
* Gets signature field layout element, which customizes the appearance of a signature.
66+
*
67+
* @return {@link SignatureFieldAppearance} layout element
68+
*/
69+
public SignatureFieldAppearance getSignatureAppearance() {
70+
return this.appearance;
71+
}
72+
73+
/**
74+
* Returns the document's certification level.
75+
* For possible values see {@link #setCertificationLevel(int)}.
76+
*
77+
* @return The certified status.
78+
*/
79+
public int getCertificationLevel() {
80+
return this.certificationLevel;
81+
}
82+
83+
/**
84+
* Sets the document's certification level.
85+
*
86+
* @param certificationLevel a new certification level for a document.
87+
* Possible values are: <ul>
88+
* <li>{@link PdfSigner#NOT_CERTIFIED}
89+
* <li>{@link PdfSigner#CERTIFIED_NO_CHANGES_ALLOWED}
90+
* <li>{@link PdfSigner#CERTIFIED_FORM_FILLING}
91+
* <li>{@link PdfSigner#CERTIFIED_FORM_FILLING_AND_ANNOTATIONS}
92+
* </ul>
93+
*/
94+
public SignerProperties setCertificationLevel(int certificationLevel) {
95+
this.certificationLevel = certificationLevel;
96+
return this;
97+
}
98+
99+
/**
100+
* Gets the field name.
101+
*
102+
* @return the field name
103+
*/
104+
public String getFieldName() {
105+
return fieldName;
106+
}
107+
108+
/**
109+
* Sets the name indicating the field to be signed. The field can already be presented in the
110+
* document but shall not be signed. If the field is not presented in the document, it will be created.
111+
*
112+
* @param fieldName The name indicating the field to be signed.
113+
*/
114+
public SignerProperties setFieldName(String fieldName) {
115+
this.fieldName = fieldName;
116+
return this;
117+
}
118+
119+
/**
120+
* Provides the page number of the signature field which this signature
121+
* appearance is associated with.
122+
*
123+
* @return The page number of the signature field which this signature
124+
* appearance is associated with.
125+
*/
126+
public int getPageNumber() {
127+
return this.pageNumber;
128+
}
129+
130+
/**
131+
* Sets the page number of the signature field which this signature
132+
* appearance is associated with. Implicitly calls {@link PdfSigner#setPageRect}
133+
* which considers page number to process the rectangle correctly.
134+
*
135+
* @param pageNumber The page number of the signature field which
136+
* this signature appearance is associated with.
137+
*
138+
* @return this instance to support fluent interface.
139+
*/
140+
public SignerProperties setPageNumber(int pageNumber) {
141+
this.pageNumber = pageNumber;
142+
return this;
143+
}
144+
145+
/**
146+
* Provides the rectangle that represent the position and dimension
147+
* of the signature field in the page.
148+
*
149+
* @return the rectangle that represent the position and dimension
150+
* of the signature field in the page
151+
*/
152+
public Rectangle getPageRect() {
153+
return this.pageRect;
154+
}
155+
156+
/**
157+
* Sets the rectangle that represent the position and dimension of
158+
* the signature field in the page.
159+
*
160+
* @param pageRect The rectangle that represents the position and
161+
* dimension of the signature field in the page.
162+
*
163+
* @return this instance to support fluent interface.
164+
*/
165+
public SignerProperties setPageRect(Rectangle pageRect) {
166+
this.pageRect = pageRect;
167+
return this;
168+
}
169+
170+
/**
171+
* Getter for the field lock dictionary.
172+
*
173+
* @return Field lock dictionary.
174+
*/
175+
public PdfSigFieldLock getFieldLockDict() {
176+
return fieldLock;
177+
}
178+
179+
/**
180+
* Setter for the field lock dictionary.
181+
* <p>
182+
* <strong>Be aware:</strong> if a signature is created on an existing signature field,
183+
* then its /Lock dictionary takes the precedence (if it exists).
184+
*
185+
* @param fieldLock Field lock dictionary
186+
*/
187+
public SignerProperties setFieldLockDict(PdfSigFieldLock fieldLock) {
188+
this.fieldLock = fieldLock;
189+
return this;
190+
}
191+
192+
/**
193+
* Returns the signature creator.
194+
*
195+
* @return The signature creator.
196+
*/
197+
public String getSignatureCreator() {
198+
return this.signatureCreator;
199+
}
200+
201+
/**
202+
* Sets the name of the application used to create the signature.
203+
*
204+
* @param signatureCreator A new name of the application signing a document.
205+
*
206+
* @return this instance to support fluent interface.
207+
*/
208+
public SignerProperties setSignatureCreator(String signatureCreator) {
209+
this.signatureCreator = signatureCreator;
210+
return this;
211+
}
212+
213+
/**
214+
* Returns the signing contact.
215+
*
216+
* @return The signing contact.
217+
*/
218+
public String getContact() {
219+
return this.contact;
220+
}
221+
222+
/**
223+
* Sets the signing contact.
224+
*
225+
* @param contact A new signing contact.
226+
*
227+
* @return this instance to support fluent interface.
228+
*/
229+
public SignerProperties setContact(String contact) {
230+
this.contact = contact;
231+
return this;
232+
}
233+
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.commons.bouncycastle.operator.AbstractOperatorCreationException;
2828
import com.itextpdf.commons.bouncycastle.pkcs.AbstractPKCSException;
2929
import com.itextpdf.commons.utils.FileUtil;
30+
import com.itextpdf.forms.form.element.SignatureFieldAppearance;
3031
import com.itextpdf.io.logs.IoLogMessageConstant;
3132
import com.itextpdf.kernel.geom.Rectangle;
3233
import com.itextpdf.kernel.pdf.PdfReader;
@@ -36,6 +37,7 @@ This file is part of the iText (R) project.
3637
import com.itextpdf.signatures.PdfPadesSigner;
3738
import com.itextpdf.signatures.PdfSigner;
3839
import com.itextpdf.signatures.PrivateKeySignature;
40+
import com.itextpdf.signatures.SignerProperties;
3941
import com.itextpdf.signatures.testutils.PemFileHelper;
4042
import com.itextpdf.signatures.testutils.SignaturesCompareTool;
4143
import com.itextpdf.signatures.testutils.TimeTestUtil;
@@ -159,9 +161,9 @@ public void signWithAdvancedClientsTest()
159161
testCrlClient.addBuilderForCertIssuer((X509Certificate) signRsaChain[0], crlBuilderMainCert);
160162
testCrlClient.addBuilderForCertIssuer((X509Certificate) signRsaChain[1], crlBuilderRootCert);
161163

162-
PdfSigner signer = createPdfSigner(srcFileName, outFileName);
164+
SignerProperties signer = createSignerProperties();
163165

164-
PdfPadesSigner padesSigner = new PdfPadesSigner();
166+
PdfPadesSigner padesSigner = createPdfPadesSigner(srcFileName, outFileName);
165167
padesSigner.setOcspClient(testOcspClient);
166168
padesSigner.setCrlClient(testCrlClient);
167169

@@ -174,15 +176,19 @@ public void signWithAdvancedClientsTest()
174176
Assert.assertNull(SignaturesCompareTool.compareSignatures(outFileName, cmpFileName));
175177
}
176178

177-
private PdfSigner createPdfSigner(String srcFileName, String outFileName) throws IOException {
178-
PdfSigner signer = new PdfSigner(new PdfReader(srcFileName), FileUtil.getFileOutputStream(outFileName),
179-
new StampingProperties());
180-
signer.setFieldName("Signature1");
181-
signer.getSignatureAppearance()
182-
.setPageRect(new Rectangle(50, 650, 200, 100))
183-
.setReason("Test")
184-
.setLocation("TestCity")
185-
.setLayer2Text("Approval test signature.\nCreated by iText.");
186-
return signer;
179+
private SignerProperties createSignerProperties() {
180+
SignerProperties signerProperties = new SignerProperties();
181+
signerProperties.setFieldName("Signature1");
182+
SignatureFieldAppearance appearance = new SignatureFieldAppearance(signerProperties.getFieldName())
183+
.setContent("Approval test signature.\nCreated by iText.");
184+
signerProperties.setPageRect(new Rectangle(50, 650, 200, 100))
185+
.setSignatureAppearance(appearance);
186+
187+
return signerProperties;
188+
}
189+
190+
private PdfPadesSigner createPdfPadesSigner(String srcFileName, String outFileName) throws IOException {
191+
return new PdfPadesSigner(new PdfReader(FileUtil.getInputStreamForFile(srcFileName)),
192+
FileUtil.getFileOutputStream(outFileName));
187193
}
188194
}

0 commit comments

Comments
 (0)