Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Sep 27, 2024
2 parents fa0057a + bb85c49 commit 2f97882
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand All @@ -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) {
}

Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public virtual String GetReasonLine() {
/// instance.
/// </returns>
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;
}

Expand All @@ -89,14 +92,17 @@ public virtual String GetLocationLine() {
/// <c>PdfSigner#setLocation</c>
/// will be used.
/// </remarks>
/// <param name="location">new signing location.</param>
/// <param name="location">new signing location</param>
/// <returns>
/// this same
/// <see cref="SignedAppearanceText"/>
/// instance.
/// instance
/// </returns>
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;
}

Expand All @@ -106,19 +112,22 @@ public virtual iText.Forms.Fields.Properties.SignedAppearanceText SetLocationLin
/// <para />
/// Note, that the signer name will be replaced by the one from the signing certificate during the actual signing.
/// </remarks>
/// <param name="signedBy">name of the signer.</param>
/// <param name="signedBy">name of the signer</param>
/// <returns>
/// this same
/// <see cref="SignedAppearanceText"/>
/// instance.
/// instance
/// </returns>
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;
}

/// <summary>Gets the name of the signer from the certificate.</summary>
/// <returns>signedBy name of the signer.</returns>
/// <returns>signedBy name of the signer</returns>
public virtual String GetSignedBy() {
return signedBy;
}
Expand All @@ -137,11 +146,11 @@ public virtual DateTime GetSignDate() {
/// <c>PdfSigner</c>
/// during the signing.
/// </remarks>
/// <param name="signDate">new signature date.</param>
/// <param name="signDate">new signature date</param>
/// <returns>
/// this same
/// <see cref="SignedAppearanceText"/>
/// instance.
/// instance
/// </returns>
public virtual iText.Forms.Fields.Properties.SignedAppearanceText SetSignDate(DateTime signDate) {
this.signDate = signDate;
Expand All @@ -150,7 +159,7 @@ public virtual iText.Forms.Fields.Properties.SignedAppearanceText SetSignDate(Da
}

/// <summary>Generates the signature description text based on the provided parameters.</summary>
/// <returns>signature description.</returns>
/// <returns>signature description</returns>
public virtual String GenerateDescriptionText() {
StringBuilder buf = new StringBuilder();
if (signedBy != null && !String.IsNullOrEmpty(signedBy)) {
Expand Down
48 changes: 33 additions & 15 deletions itext/itext.sign/itext/signatures/PdfSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PdfName, int?> exc = new Dictionary<PdfName, int?>();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
7 changes: 7 additions & 0 deletions itext/itext.sign/itext/signatures/SignerProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ You should have received a copy of the GNU Affero General Public License
namespace iText.Signatures {
/// <summary>Properties to be used in signing operations.</summary>
public class SignerProperties {
/// <summary>
/// This string could be used to create the
/// <see cref="iText.Forms.Form.Element.SignatureFieldAppearance"/>
/// 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.
/// </summary>
/// <seealso cref="SetSignatureAppearance(iText.Forms.Form.Element.SignatureFieldAppearance)"/>
public const String IGNORED_ID = "";

private PdfSigFieldLock fieldLock;
Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ad761fce7804db0efa8672f4fe236215a6c491d0
dc76524bdb9871dfcd4e2b6d156e42a653d2396d

0 comments on commit 2f97882

Please sign in to comment.