diff --git a/itext.tests/itext.forms.tests/itext/forms/form/element/SignatureFieldAppearanceTest.cs b/itext.tests/itext.forms.tests/itext/forms/form/element/SignatureFieldAppearanceTest.cs index b6d208abf3..0c6ea92e59 100644 --- a/itext.tests/itext.forms.tests/itext/forms/form/element/SignatureFieldAppearanceTest.cs +++ b/itext.tests/itext.forms.tests/itext/forms/form/element/SignatureFieldAppearanceTest.cs @@ -124,6 +124,38 @@ public virtual void CustomizedSigFieldTest() { NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER)); } + [NUnit.Framework.Test] + public virtual void EmptySigFieldAppearanceTest() { + String outPdf = DESTINATION_FOLDER + "emptySigFieldAppearance.pdf"; + String cmpPdf = SOURCE_FOLDER + "cmp_emptySigFieldAppearance.pdf"; + using (Document document = new Document(new PdfDocument(new PdfWriter(outPdf)))) { + SignedAppearanceText description = new SignedAppearanceText().SetSignedBy(null).SetLocationLine(null).SetReasonLine + (null); + SignatureFieldAppearance formSigField = new SignatureFieldAppearance("Signature1").SetContent(description); + formSigField.SetBackgroundColor(ColorConstants.LIGHT_GRAY); + formSigField.SetBorder(new SolidBorder(ColorConstants.GREEN, 2)); + formSigField.SetHeight(100).SetWidth(200); + document.Add(formSigField); + } + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER)); + } + + [NUnit.Framework.Test] + public virtual void IgnoreSignDateAndReasonInAppearanceTest() { + String outPdf = DESTINATION_FOLDER + "ignoreSignDateAndReasonInAppearance.pdf"; + String cmpPdf = SOURCE_FOLDER + "cmp_ignoreSignDateAndReasonInAppearance.pdf"; + using (Document document = new Document(new PdfDocument(new PdfWriter(outPdf)))) { + SignedAppearanceText description = new SignedAppearanceText().SetSignedBy("Signer Name").SetLocationLine("Test Location" + ).SetReasonLine(null); + SignatureFieldAppearance formSigField = new SignatureFieldAppearance("Signature1").SetContent(description); + formSigField.SetBackgroundColor(ColorConstants.LIGHT_GRAY); + formSigField.SetBorder(new SolidBorder(ColorConstants.GREEN, 2)); + formSigField.SetHeight(100).SetWidth(200); + document.Add(formSigField); + } + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER)); + } + [NUnit.Framework.Test] [LogMessage(iText.IO.Logs.IoLogMessageConstant.CLIP_ELEMENT)] public virtual void SignatureFieldVerticalAlignmentTest() { @@ -341,7 +373,7 @@ public virtual void FontSizeTest() { [NUnit.Framework.Test] public virtual void FontNullCustomCheck() { String outPdf = DESTINATION_FOLDER + "fontNullCustomCheck.pdf"; - PdfDocument pdfDoc = new _PdfDocument_412(new PdfWriter(outPdf)); + PdfDocument pdfDoc = new _PdfDocument_450(new PdfWriter(outPdf)); Document document = new Document(pdfDoc); SignatureFieldAppearance sigField = new SignatureFieldAppearance("SigField"); sigField.SetContent("test"); @@ -352,8 +384,8 @@ public virtual void FontNullCustomCheck() { NUnit.Framework.Assert.AreEqual(LayoutExceptionMessageConstant.INVALID_FONT_PROPERTY_VALUE, e.Message); } - private sealed class _PdfDocument_412 : PdfDocument { - public _PdfDocument_412(PdfWriter baseArg1) + private sealed class _PdfDocument_450 : PdfDocument { + public _PdfDocument_450(PdfWriter baseArg1) : base(baseArg1) { } diff --git a/itext.tests/itext.forms.tests/resources/itext/forms/form/element/SignatureFieldAppearanceTest/cmp_emptySigFieldAppearance.pdf b/itext.tests/itext.forms.tests/resources/itext/forms/form/element/SignatureFieldAppearanceTest/cmp_emptySigFieldAppearance.pdf new file mode 100644 index 0000000000..9110617e77 Binary files /dev/null and b/itext.tests/itext.forms.tests/resources/itext/forms/form/element/SignatureFieldAppearanceTest/cmp_emptySigFieldAppearance.pdf differ diff --git a/itext.tests/itext.forms.tests/resources/itext/forms/form/element/SignatureFieldAppearanceTest/cmp_ignoreSignDateAndReasonInAppearance.pdf b/itext.tests/itext.forms.tests/resources/itext/forms/form/element/SignatureFieldAppearanceTest/cmp_ignoreSignDateAndReasonInAppearance.pdf new file mode 100644 index 0000000000..8b781d8589 Binary files /dev/null and b/itext.tests/itext.forms.tests/resources/itext/forms/form/element/SignatureFieldAppearanceTest/cmp_ignoreSignDateAndReasonInAppearance.pdf differ diff --git a/itext.tests/itext.sign.tests/itext/signatures/sign/SignedAppearanceTextTest.cs b/itext.tests/itext.sign.tests/itext/signatures/sign/SignedAppearanceTextTest.cs index fd9fc54149..a085ae28a4 100644 --- a/itext.tests/itext.sign.tests/itext/signatures/sign/SignedAppearanceTextTest.cs +++ b/itext.tests/itext.sign.tests/itext/signatures/sign/SignedAppearanceTextTest.cs @@ -101,6 +101,30 @@ public virtual void DefaultSignedAppearanceTextTest() { NUnit.Framework.Assert.IsNull(SignaturesCompareTool.CompareSignatures(outPdf, cmpPdf)); } + [NUnit.Framework.Test] + public virtual void NoReasonLocationSignDateInAppearanceTextTest() { + String srcFile = SOURCE_FOLDER + "simpleDocument.pdf"; + String cmpPdf = SOURCE_FOLDER + "cmp_noReasonLocationSignDateInAppearanceText.pdf"; + String outPdf = DESTINATION_FOLDER + "noReasonLocationSignDateInAppearanceText.pdf"; + Rectangle rect = new Rectangle(36, 648, 200, 100); + String fieldName = "Signature1"; + SignatureFieldAppearance appearance = new SignatureFieldAppearance(SignerProperties.IGNORED_ID).SetContent + (new SignedAppearanceText().SetReasonLine(null).SetLocationLine(null)); + PdfSigner signer = new PdfSigner(new PdfReader(srcFile), FileUtil.GetFileOutputStream(outPdf), new StampingProperties + ()); + SignerProperties signerProperties = new SignerProperties().SetCertificationLevel(AccessPermissions.UNSPECIFIED + ).SetFieldName(fieldName).SetReason("Test 1").SetLocation("TestCity 1").SetSignatureAppearance(appearance + ).SetClaimedSignDate((DateTime)TimestampConstants.UNDEFINED_TIMESTAMP_DATE).SetPageRect(rect); + signer.SetSignerProperties(signerProperties); + // Creating the signature + IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256); + signer.SignDetached(new BouncyCastleDigest(), pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES + ); + NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_" + , GetTestMap(new Rectangle(36, 676, 200, 15)))); + NUnit.Framework.Assert.IsNull(SignaturesCompareTool.CompareSignatures(outPdf, cmpPdf)); + } + [NUnit.Framework.Test] public virtual void SignPDFADocumentWithoutSettingFont() { String srcFile = DESTINATION_FOLDER + "simplePDFA.pdf"; diff --git a/itext.tests/itext.sign.tests/resources/itext/signatures/sign/SignedAppearanceTextTest/cmp_noReasonLocationSignDateInAppearanceText.pdf b/itext.tests/itext.sign.tests/resources/itext/signatures/sign/SignedAppearanceTextTest/cmp_noReasonLocationSignDateInAppearanceText.pdf new file mode 100644 index 0000000000..724bfc6182 Binary files /dev/null and b/itext.tests/itext.sign.tests/resources/itext/signatures/sign/SignedAppearanceTextTest/cmp_noReasonLocationSignDateInAppearanceText.pdf differ diff --git a/itext/itext.forms/itext/forms/fields/properties/SignedAppearanceText.cs b/itext/itext.forms/itext/forms/fields/properties/SignedAppearanceText.cs index 6d31c79006..8e2f0337d1 100644 --- a/itext/itext.forms/itext/forms/fields/properties/SignedAppearanceText.cs +++ b/itext/itext.forms/itext/forms/fields/properties/SignedAppearanceText.cs @@ -71,7 +71,10 @@ public virtual String GetReasonLine() { /// instance. /// public virtual iText.Forms.Fields.Properties.SignedAppearanceText SetReasonLine(String reason) { - this.reason = reason.Trim(); + if (reason != null) { + reason = reason.Trim(); + } + this.reason = reason; return this; } @@ -89,14 +92,17 @@ public virtual String GetLocationLine() { /// PdfSigner#setLocation /// will be used. /// - /// new signing location. + /// new signing location /// /// this same /// - /// instance. + /// instance /// public virtual iText.Forms.Fields.Properties.SignedAppearanceText SetLocationLine(String location) { - this.location = location.Trim(); + if (location != null) { + location = location.Trim(); + } + this.location = location; return this; } @@ -106,19 +112,22 @@ public virtual iText.Forms.Fields.Properties.SignedAppearanceText SetLocationLin /// /// Note, that the signer name will be replaced by the one from the signing certificate during the actual signing. /// - /// name of the signer. + /// name of the signer /// /// this same /// - /// instance. + /// instance /// public virtual iText.Forms.Fields.Properties.SignedAppearanceText SetSignedBy(String signedBy) { - this.signedBy = signedBy.Trim(); + if (signedBy != null) { + signedBy = signedBy.Trim(); + } + this.signedBy = signedBy; return this; } /// Gets the name of the signer from the certificate. - /// signedBy name of the signer. + /// signedBy name of the signer public virtual String GetSignedBy() { return signedBy; } @@ -137,11 +146,11 @@ public virtual DateTime GetSignDate() { /// PdfSigner /// during the signing. /// - /// new signature date. + /// new signature date /// /// this same /// - /// instance. + /// instance /// public virtual iText.Forms.Fields.Properties.SignedAppearanceText SetSignDate(DateTime signDate) { this.signDate = signDate; @@ -150,7 +159,7 @@ public virtual iText.Forms.Fields.Properties.SignedAppearanceText SetSignDate(Da } /// Generates the signature description text based on the provided parameters. - /// signature description. + /// signature description public virtual String GenerateDescriptionText() { StringBuilder buf = new StringBuilder(); if (signedBy != null && !String.IsNullOrEmpty(signedBy)) { diff --git a/itext/itext.sign/itext/signatures/PdfSigner.cs b/itext/itext.sign/itext/signatures/PdfSigner.cs index c69690a944..7130cead10 100644 --- a/itext/itext.sign/itext/signatures/PdfSigner.cs +++ b/itext/itext.sign/itext/signatures/PdfSigner.cs @@ -530,7 +530,10 @@ public virtual void SignDetached(IExternalDigest externalDigest, IExternalSignat dic.SetLocation(this.signerProperties.GetLocation()); dic.SetSignatureCreator(this.signerProperties.GetSignatureCreator()); dic.SetContact(this.signerProperties.GetContact()); - dic.SetDate(new PdfDate(this.signerProperties.GetClaimedSignDate())); + DateTime claimedSignDate = this.signerProperties.GetClaimedSignDate(); + if (claimedSignDate != TimestampConstants.UNDEFINED_TIMESTAMP_DATE) { + dic.SetDate(new PdfDate(claimedSignDate)); + } // time-stamp will over-rule this cryptoDictionary = dic; IDictionary exc = new Dictionary(); @@ -1254,8 +1257,9 @@ internal virtual PdfSignature CreateSignatureDictionary(bool includeDate) { dic.SetLocation(this.signerProperties.GetLocation()); dic.SetSignatureCreator(this.signerProperties.GetSignatureCreator()); dic.SetContact(this.signerProperties.GetContact()); - if (includeDate) { - dic.SetDate(new PdfDate(this.signerProperties.GetClaimedSignDate())); + DateTime claimedSignDate = this.signerProperties.GetClaimedSignDate(); + if (includeDate && claimedSignDate != TimestampConstants.UNDEFINED_TIMESTAMP_DATE) { + dic.SetDate(new PdfDate(claimedSignDate)); } // time-stamp will over-rule this return dic; @@ -1310,23 +1314,37 @@ private void SetContent() { } private SignedAppearanceText GenerateSignatureText() { - return new SignedAppearanceText().SetSignedBy(signerName).SetSignDate(this.signerProperties.GetClaimedSignDate - ()).SetReasonLine("Reason: " + this.signerProperties.GetReason()).SetLocationLine("Location: " + this. - signerProperties.GetLocation()); + SignedAppearanceText signedAppearanceText = new SignedAppearanceText(); + FillInAppearanceText(signedAppearanceText); + return signedAppearanceText; } private void PopulateExistingModelElement() { this.signerProperties.GetSignatureAppearance().SetSignerName(signerName); - SignedAppearanceText signedAppearanceText = this.signerProperties.GetSignatureAppearance().GetSignedAppearanceText + SignedAppearanceText appearanceText = this.signerProperties.GetSignatureAppearance().GetSignedAppearanceText (); - if (signedAppearanceText != null) { - signedAppearanceText.SetSignedBy(signerName).SetSignDate(this.signerProperties.GetClaimedSignDate()); - if (String.IsNullOrEmpty(signedAppearanceText.GetReasonLine())) { - signedAppearanceText.SetReasonLine("Reason: " + this.signerProperties.GetReason()); - } - if (String.IsNullOrEmpty(signedAppearanceText.GetLocationLine())) { - signedAppearanceText.SetLocationLine("Location: " + this.signerProperties.GetLocation()); - } + if (appearanceText != null) { + FillInAppearanceText(appearanceText); + } + } + + private void FillInAppearanceText(SignedAppearanceText appearanceText) { + appearanceText.SetSignedBy(signerName); + DateTime claimedSignDate = this.signerProperties.GetClaimedSignDate(); + if (claimedSignDate != TimestampConstants.UNDEFINED_TIMESTAMP_DATE) { + appearanceText.SetSignDate(claimedSignDate); + } + String reason = signerProperties.GetReason(); + bool setReason = appearanceText.GetReasonLine() != null && String.IsNullOrEmpty(appearanceText.GetReasonLine + ()); + if (setReason && reason != null && !String.IsNullOrEmpty(reason)) { + appearanceText.SetReasonLine("Reason: " + reason); + } + String location = signerProperties.GetLocation(); + bool setLocation = appearanceText.GetLocationLine() != null && String.IsNullOrEmpty(appearanceText.GetLocationLine + ()); + if (setLocation && location != null && !String.IsNullOrEmpty(location)) { + appearanceText.SetLocationLine("Location: " + location); } } diff --git a/itext/itext.sign/itext/signatures/SignerProperties.cs b/itext/itext.sign/itext/signatures/SignerProperties.cs index 23c786e6c3..adf97890e5 100644 --- a/itext/itext.sign/itext/signatures/SignerProperties.cs +++ b/itext/itext.sign/itext/signatures/SignerProperties.cs @@ -29,6 +29,13 @@ You should have received a copy of the GNU Affero General Public License namespace iText.Signatures { /// Properties to be used in signing operations. public class SignerProperties { + /// + /// This string could be used to create the + /// + /// instance which will be used for signing + /// since its ID will be ignored anyway in that case, and specified ID won't override the field name. + /// + /// public const String IGNORED_ID = ""; private PdfSigFieldLock fieldLock; diff --git a/port-hash b/port-hash index e5da40ac4b..a523416cfd 100644 --- a/port-hash +++ b/port-hash @@ -1 +1 @@ -ad761fce7804db0efa8672f4fe236215a6c491d0 \ No newline at end of file +dc76524bdb9871dfcd4e2b6d156e42a653d2396d