Skip to content

Commit b31652c

Browse files
committed
Builder-like pattern for PdfSignatureAppearance
DEVSIX-391
1 parent 9be7a20 commit b31652c

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

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

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ public int getPageNumber() {
190190
* @param pageNumber The page number of the signature field which
191191
* this signature appearance is associated with.
192192
*/
193-
public void setPageNumber(int pageNumber) {
193+
public PdfSignatureAppearance setPageNumber(int pageNumber) {
194194
this.page = pageNumber;
195+
return this;
195196
}
196197

197198
/**
@@ -212,9 +213,10 @@ public Rectangle getPageRect() {
212213
* @param pageRect The rectangle that represents the position and
213214
* dimension of the signature field in the page.
214215
*/
215-
public void setPageRect(Rectangle pageRect) {
216+
public PdfSignatureAppearance setPageRect(Rectangle pageRect) {
216217
this.pageRect = new Rectangle(pageRect);
217218
this.rect = new Rectangle(pageRect.getWidth(), pageRect.getHeight());
219+
return this;
218220
}
219221

220222
/**
@@ -259,8 +261,9 @@ public RenderingMode getRenderingMode() {
259261
*
260262
* @param renderingMode the rendering mode
261263
*/
262-
public void setRenderingMode(RenderingMode renderingMode) {
264+
public PdfSignatureAppearance setRenderingMode(RenderingMode renderingMode) {
263265
this.renderingMode = renderingMode;
266+
return this;
264267
}
265268

266269
/**
@@ -277,17 +280,19 @@ public String getReason() {
277280
*
278281
* @param reason signing reason.
279282
*/
280-
public void setReason(String reason) {
283+
public PdfSignatureAppearance setReason(String reason) {
281284
this.reason = reason;
285+
return this;
282286
}
283287

284288
/**
285289
* Sets the caption for the signing reason.
286290
*
287291
* @param reasonCaption A new signing reason caption
288292
*/
289-
public void setReasonCaption(String reasonCaption) {
293+
public PdfSignatureAppearance setReasonCaption(String reasonCaption) {
290294
this.reasonCaption = reasonCaption;
295+
return this;
291296
}
292297

293298
/**
@@ -304,17 +309,19 @@ public String getLocation() {
304309
*
305310
* @param location A new signing location
306311
*/
307-
public void setLocation(String location) {
312+
public PdfSignatureAppearance setLocation(String location) {
308313
this.location = location;
314+
return this;
309315
}
310316

311317
/**
312318
* Sets the caption for the signing location.
313319
*
314320
* @param locationCaption A new signing location caption
315321
*/
316-
public void setLocationCaption(String locationCaption) {
322+
public PdfSignatureAppearance setLocationCaption(String locationCaption) {
317323
this.locationCaption = locationCaption;
324+
return this;
318325
}
319326

320327
/**
@@ -331,8 +338,9 @@ public String getSignatureCreator(){
331338
*
332339
* @param signatureCreator A new name of the application signing a document
333340
*/
334-
public void setSignatureCreator(String signatureCreator){
341+
public PdfSignatureAppearance setSignatureCreator(String signatureCreator){
335342
this.signatureCreator = signatureCreator;
343+
return this;
336344
}
337345

338346
/**
@@ -349,8 +357,9 @@ public String getContact() {
349357
*
350358
* @param contact A new signing contact
351359
*/
352-
public void setContact(String contact) {
360+
public PdfSignatureAppearance setContact(String contact) {
353361
this.contact = contact;
362+
return this;
354363
}
355364

356365
/**
@@ -359,8 +368,9 @@ public void setContact(String contact) {
359368
*
360369
* @param signCertificate the certificate
361370
*/
362-
public void setCertificate(Certificate signCertificate) {
371+
public PdfSignatureAppearance setCertificate(Certificate signCertificate) {
363372
this.signCertificate = signCertificate;
373+
return this;
364374
}
365375

366376
/**
@@ -386,15 +396,17 @@ public Image getSignatureGraphic() {
386396
*
387397
* @param signatureGraphic image rendered. If null the mode is defaulted to RenderingMode.DESCRIPTION
388398
*/
389-
public void setSignatureGraphic(Image signatureGraphic) {
399+
public PdfSignatureAppearance setSignatureGraphic(Image signatureGraphic) {
390400
this.signatureGraphic = signatureGraphic;
401+
return this;
391402
}
392403

393404
/**
394405
* Indicates that the existing appearances needs to be reused as layer 0.
395406
*/
396-
public void setReuseAppearance(boolean reuseAppearance) {
407+
public PdfSignatureAppearance setReuseAppearance(boolean reuseAppearance) {
397408
this.reuseAppearance = reuseAppearance;
409+
return this;
398410
}
399411

400412
// layer 2
@@ -413,8 +425,9 @@ public Image getImage() {
413425
*
414426
* @param image the background image for the layer 2
415427
*/
416-
public void setImage(Image image) {
428+
public PdfSignatureAppearance setImage(Image image) {
417429
this.image = image;
430+
return this;
418431
}
419432

420433
/**
@@ -434,8 +447,9 @@ public float getImageScale() {
434447
*
435448
* @param imageScale the scaling to be applied to the background image
436449
*/
437-
public void setImageScale(float imageScale) {
450+
public PdfSignatureAppearance setImageScale(float imageScale) {
438451
this.imageScale = imageScale;
452+
return this;
439453
}
440454

441455
/**
@@ -444,8 +458,9 @@ public void setImageScale(float imageScale) {
444458
* @param text the signature text identifying the signer. If null or not set
445459
* a standard description will be used
446460
*/
447-
public void setLayer2Text(String text) {
461+
public PdfSignatureAppearance setLayer2Text(String text) {
448462
layer2Text = text;
463+
return this;
449464
}
450465

451466
/**
@@ -471,17 +486,19 @@ public PdfFont getLayer2Font() {
471486
*
472487
* @param layer2Font the n2 and n4 font
473488
*/
474-
public void setLayer2Font(PdfFont layer2Font) {
489+
public PdfSignatureAppearance setLayer2Font(PdfFont layer2Font) {
475490
this.layer2Font = layer2Font;
491+
return this;
476492
}
477493

478494
/**
479495
* Sets the n2 and n4 layer font size.
480496
*
481497
* @param fontSize font size
482498
*/
483-
public void setLayer2FontSize(float fontSize) {
499+
public PdfSignatureAppearance setLayer2FontSize(float fontSize) {
484500
this.layer2FontSize = fontSize;
501+
return this;
485502
}
486503

487504
/**
@@ -779,17 +796,19 @@ protected java.util.Calendar getSignDate() {
779796
*
780797
* @param signDate A new signature date
781798
*/
782-
protected void setSignDate(java.util.Calendar signDate) {
799+
protected PdfSignatureAppearance setSignDate(java.util.Calendar signDate) {
783800
this.signDate = signDate;
801+
return this;
784802
}
785803

786804
/**
787805
* Set the field name of the appearance.
788806
*
789807
* @param fieldName name of the field
790808
*/
791-
protected void setFieldName(String fieldName) {
809+
protected PdfSignatureAppearance setFieldName(String fieldName) {
792810
this.fieldName = fieldName;
811+
return this;
793812
}
794813

795814
private void createBlankN0() {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,10 @@ protected void sign(String src, String name, String dest,
135135
PdfSigner signer = new PdfSigner(reader, new PdfWriter(dest), false);
136136

137137
// Creating the appearance
138-
PdfSignatureAppearance appearance = signer.getSignatureAppearance();
139-
appearance.setReason(reason);
140-
appearance.setLocation(location);
141-
142-
appearance.setReuseAppearance(setReuseAppearance);
138+
PdfSignatureAppearance appearance = signer.getSignatureAppearance()
139+
.setReason(reason)
140+
.setLocation(location)
141+
.setReuseAppearance(setReuseAppearance);
143142

144143
if (rectangleForNewField != null) {
145144
appearance.setPageRect(rectangleForNewField);

0 commit comments

Comments
 (0)