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 Jan 23, 2024
2 parents 7a77aac + 41cd067 commit f2fcd67
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ You should have received a copy of the GNU Affero General Public License
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using iText.Kernel.Utils;
using iText.Layout.Borders;
using iText.Layout.Properties;
using iText.Signatures;
using iText.Signatures.Testutils;
Expand Down Expand Up @@ -132,10 +133,11 @@ public virtual void TestSigningInAppendModeWithHybridDocument() {
String cmp = SOURCE_FOLDER + "cmp_signed_hybrid.pdf";
PdfSigner signer = new PdfSigner(new PdfReader(src), new FileStream(dest, FileMode.Create), new StampingProperties
().UseAppendMode());
PdfSignatureAppearance appearance = signer.GetSignatureAppearance();
appearance.SetLayer2FontSize(13.8f).SetPageRect(new Rectangle(36, 748, 200, 100)).SetPageNumber(1).SetReason
("Test").SetLocation("Nagpur");
signer.SetFieldName("Sign1");
String fieldName = "Sign1";
SignatureFieldAppearance appearance = new SignatureFieldAppearance(fieldName).SetFontSize(13.8f);
signer.SetFieldName(fieldName);
signer.SetReason("Test").SetLocation("Nagpur").SetPageRect(new Rectangle(36, 748, 200, 100)).SetPageNumber
(1).SetSignatureAppearance(appearance);
signer.SetCertificationLevel(PdfSigner.NOT_CERTIFIED);
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
Expand All @@ -154,10 +156,10 @@ public virtual void FontColorTest01() {
String src = SOURCE_FOLDER + "simpleDocument.pdf";
PdfSigner signer = new PdfSigner(new PdfReader(src), new FileStream(dest, FileMode.Create), new StampingProperties
());
// Creating the appearance
signer.GetSignatureAppearance().SetLayer2FontColor(ColorConstants.RED).SetLayer2Text("Verified and signed by me."
).SetPageRect(rect);
signer.SetFieldName("Signature1");
// Creating the appearance
signer.SetPageRect(rect).SetSignatureAppearance(new SignatureFieldAppearance(signer.GetFieldName()).SetFontColor
(ColorConstants.RED).SetContent("Verified and signed by me."));
// Creating the signature
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
Expand Down Expand Up @@ -197,9 +199,10 @@ public virtual void SignExistingNotMergedFieldNotReusedAPTest() {
PdfReader reader = new PdfReader(src);
PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties());
signer.SetCertificationLevel(PdfSigner.NOT_CERTIFIED);
signer.GetSignatureAppearance().SetLayer2Text("Verified and signed by me.").SetReason("Test 1").SetLocation
("TestCity").SetReuseAppearance(false);
signer.SetFieldName("Signature1");
signer.SetReason("Test 1").SetLocation("TestCity").SetSignatureAppearance(new SignatureFieldAppearance(signer
.GetFieldName()).SetContent("Verified and signed by me."));
signer.GetSignatureField().SetReuseAppearance(false);
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(dest, SOURCE_FOLDER + "cmp_" + fileName, DESTINATION_FOLDER
Expand All @@ -216,9 +219,11 @@ public virtual void SignExistingNotMergedFieldReusedAPTest() {
PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties());
signer.SetCertificationLevel(PdfSigner.NOT_CERTIFIED);
signer.SetFieldName("Signature1");
signer.GetSignatureAppearance().SetLayer2Text("SIGNED").SetLayer2FontColor(ColorConstants.GREEN).SetReason
("Test 1").SetLocation("TestCity").SetReuseAppearance(true).GetSignatureAppearance().SetProperty(Property
.VERTICAL_ALIGNMENT, VerticalAlignment.MIDDLE);
SignatureFieldAppearance appearance = new SignatureFieldAppearance(signer.GetFieldName()).SetContent("SIGNED"
).SetFontColor(ColorConstants.GREEN);
appearance.SetProperty(Property.VERTICAL_ALIGNMENT, VerticalAlignment.MIDDLE);
signer.SetReason("Test 1").SetLocation("TestCity").SetSignatureAppearance(appearance);
signer.GetSignatureField().SetReuseAppearance(true);
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(dest, SOURCE_FOLDER + "cmp_" + fileName, DESTINATION_FOLDER
Expand All @@ -234,9 +239,10 @@ public virtual void SignExistingNotMergedFieldReusedAPEntryNDicTest() {
PdfReader reader = new PdfReader(src);
PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties());
signer.SetCertificationLevel(PdfSigner.NOT_CERTIFIED);
signer.GetSignatureAppearance().SetLayer2Text("Verified and signed by me.").SetReason("Test 1").SetLocation
("TestCity").SetReuseAppearance(true);
signer.SetFieldName("Signature1");
signer.SetReason("Test 1").SetLocation("TestCity").SetSignatureAppearance(new SignatureFieldAppearance(signer
.GetFieldName()).SetContent("Verified and signed by me."));
signer.GetSignatureField().SetReuseAppearance(true);
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(dest, SOURCE_FOLDER + "cmp_" + fileName, DESTINATION_FOLDER
Expand Down Expand Up @@ -372,18 +378,20 @@ public virtual void Layer2Test() {
public virtual void CreateAndSignSignatureFieldTest() {
String src = SOURCE_FOLDER + "noSignatureField.pdf";
String dest = DESTINATION_FOLDER + "createdAndSignedSignatureField.pdf";
String fieldName = "Signature1";
String unsignedDoc = DESTINATION_FOLDER + "unsignedSignatureField.pdf";
PdfDocument document = new PdfDocument(new PdfReader(src), new PdfWriter(unsignedDoc));
PdfSignatureFormField field = new SignatureFormFieldBuilder(document, "Signature1").SetPage(1).SetWidgetRectangle
PdfSignatureFormField field = new SignatureFormFieldBuilder(document, fieldName).SetPage(1).SetWidgetRectangle
(new Rectangle(45, 509, 517, 179)).CreateSignature();
PdfFormCreator.GetAcroForm(document, true).AddField(field);
document.Close();
PdfSigner signer = new PdfSigner(new PdfReader(unsignedDoc), new FileStream(dest, FileMode.Create), new StampingProperties
());
signer.SetFieldName(fieldName);
// Creating the appearance
signer.GetSignatureAppearance().SetReason("Appearance is tested").SetLocation("TestCity").SetLayer2Text("Test signature field appearance. Test signature field appearance. "
SignatureFieldAppearance appearance = new SignatureFieldAppearance(fieldName).SetContent("Test signature field appearance. Test signature field appearance. "
+ "Test signature field appearance. Test signature field appearance");
signer.SetFieldName("Signature1");
signer.SetReason("Appearance is tested").SetLocation("TestCity").SetSignatureAppearance(appearance);
// Signing
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
Expand All @@ -397,11 +405,12 @@ public virtual void SignExistedSignatureFieldTest() {
String dest = DESTINATION_FOLDER + fileName;
PdfSigner signer = new PdfSigner(new PdfReader(src), new FileStream(dest, FileMode.Create), new StampingProperties
());
// Creating the appearance
signer.GetSignatureAppearance().SetReason("Appearance is tested").SetLocation("TestCity").SetReuseAppearance
(true).SetLayer2Text("Test signature field appearance. Test signature field appearance. " + "Test signature field appearance. Test signature field appearance"
);
signer.SetFieldName("Signature1");
// Creating the appearance
SignatureFieldAppearance appearance = new SignatureFieldAppearance(signer.GetFieldName()).SetContent("Test signature field appearance. Test signature field appearance. "
+ "Test signature field appearance. Test signature field appearance");
signer.SetReason("Appearance is tested").SetLocation("TestCity").SetSignatureAppearance(appearance);
signer.GetSignatureField().SetReuseAppearance(true);
// Signing
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
Expand Down Expand Up @@ -446,6 +455,28 @@ public virtual void DeprecatedLayersTest() {
TestLayers(src, fileName, true);
}

[NUnit.Framework.Test]
public virtual void SignatureFieldAppearanceTest() {
String fileName = "signatureFieldAppearanceTest.pdf";
String src = SOURCE_FOLDER + "noSignatureField.pdf";
String cmp = SOURCE_FOLDER + "cmp_" + fileName;
String dest = DESTINATION_FOLDER + fileName;
String fieldName = "Signature1";
PdfSigner signer = new PdfSigner(new PdfReader(src), new FileStream(dest, FileMode.Create), new StampingProperties
());
signer.SetFieldName(fieldName);
SignatureFieldAppearance appearance = new SignatureFieldAppearance(fieldName).SetContent("Signature field"
).SetBackgroundColor(ColorConstants.GREEN).SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 3)).SetFontColor
(ColorConstants.DARK_GRAY).SetFontSize(20).SetTextAlignment(TextAlignment.CENTER);
signer.SetPageRect(new Rectangle(250, 500, 100, 100)).SetReason("Test 1").SetLocation("TestCity").SetSignatureAppearance
(appearance);
// Signing
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
NUnit.Framework.Assert.IsNull(SignaturesCompareTool.CompareSignatures(dest, cmp));
NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(dest, cmp, DESTINATION_FOLDER, "diff_"));
}

private static void CompareSignatureAppearances(String outPdf, String cmpPdf) {
ITextTest.PrintOutCmpPdfNameAndDir(outPdf, cmpPdf);
using (PdfDocument outDoc = new PdfDocument(new PdfReader(outPdf))) {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ You should have received a copy of the GNU Affero General Public License
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using iText.Commons;
using iText.Forms.Fields;
using iText.Forms.Form;
using iText.Forms.Form.Element;
using iText.Forms.Logs;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Pdf.Tagging;
using iText.Kernel.Pdf.Tagutils;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Layout;
using iText.Layout.Minmaxwidth;
using iText.Layout.Properties;
Expand Down Expand Up @@ -300,64 +297,6 @@ internal virtual void ApplyProperties(IDictionary<int, Object> properties) {
}
}

/// <summary>Applies the border property.</summary>
/// <param name="annotation">the annotation to set border characteristics to.</param>
internal virtual void ApplyBorderProperty(PdfFormAnnotation annotation) {
ApplyBorderProperty(this, annotation);
}

/// <summary>Applies the border property to the renderer.</summary>
/// <param name="renderer">renderer to apply border properties to.</param>
/// <param name="annotation">the annotation to set border characteristics to.</param>
internal static void ApplyBorderProperty(IRenderer renderer, PdfFormAnnotation annotation) {
Border border = renderer.GetProperty<Border>(Property.BORDER);
if (border == null) {
// For now, we set left border to an annotation, but appropriate borders for an element will be drawn.
border = renderer.GetProperty<Border>(Property.BORDER_LEFT);
}
if (border != null) {
annotation.SetBorderStyle(TransformBorderTypeToBorderStyleDictionary(border.GetBorderType()));
annotation.SetBorderColor(border.GetColor());
annotation.SetBorderWidth(border.GetWidth());
}
}

private static PdfDictionary TransformBorderTypeToBorderStyleDictionary(int borderType) {
PdfDictionary bs = new PdfDictionary();
PdfName style;
switch (borderType) {
case 1001: {
style = PdfAnnotation.STYLE_UNDERLINE;
break;
}

case 1002: {
style = PdfAnnotation.STYLE_BEVELED;
break;
}

case 1003: {
style = PdfAnnotation.STYLE_INSET;
break;
}

case Border.DASHED_FIXED:
case Border.DASHED:
case Border.DOTTED: {
// Default dash array will be used.
style = PdfAnnotation.STYLE_DASHED;
break;
}

default: {
style = PdfAnnotation.STYLE_SOLID;
break;
}
}
bs.Put(PdfName.S, style);
return bs;
}

private void ProcessLangAttribute() {
IPropertyContainer propertyContainer = flatRenderer.GetModelElement();
String lang = GetLang();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ You should have received a copy of the GNU Affero General Public License
using System.Collections.Generic;
using iText.Forms.Fields;
using iText.Forms.Form.Element;
using iText.Forms.Util;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf.Annot;
Expand Down Expand Up @@ -74,7 +75,7 @@ internal virtual void ApplyDefaultFieldProperties(PdfFormField inputField) {
inputField.SetColor(color.GetColor());
}
inputField.SetJustification(this.GetProperty<TextAlignment?>(Property.TEXT_ALIGNMENT));
ApplyBorderProperty(inputField.GetFirstFormAnnotation());
BorderStyleUtil.ApplyBorderProperty(this, inputField.GetFirstFormAnnotation());
Background background = this.GetProperty<Background>(Property.BACKGROUND);
if (background != null) {
inputField.GetFirstFormAnnotation().SetBackgroundColor(background.GetColor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ You should have received a copy of the GNU Affero General Public License
using iText.Forms.Form;
using iText.Forms.Form.Element;
using iText.Forms.Form.Renderer.Checkboximpl;
using iText.Forms.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout.Borders;
Expand Down Expand Up @@ -182,7 +183,7 @@ protected internal override void ApplyAcroField(DrawContext drawContext) {
}
PdfButtonFormField checkBox = builder.CreateCheckBox();
checkBox.DisableFieldRegeneration();
ApplyBorderProperty(checkBox.GetFirstFormAnnotation());
BorderStyleUtil.ApplyBorderProperty(this, checkBox.GetFirstFormAnnotation());
Background background = this.modelElement.GetProperty<Background>(Property.BACKGROUND);
if (background != null) {
checkBox.GetFirstFormAnnotation().SetBackgroundColor(background.GetColor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected internal override void ApplyAcroField(DrawContext drawContext) {
if (background != null) {
radio.SetBackgroundColor(background.GetColor());
}
ApplyBorderProperty(radio);
BorderStyleUtil.ApplyBorderProperty(this, radio);
radio.SetFormFieldElement((Radio)modelElement);
radioGroup.AddKid(radio);
radioGroup.EnableFieldRegeneration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ You should have received a copy of the GNU Affero General Public License
using iText.Forms.Fields;
using iText.Forms.Form;
using iText.Forms.Form.Element;
using iText.Forms.Util;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
Expand Down Expand Up @@ -104,7 +105,7 @@ protected internal override void ApplyAcroField(DrawContext drawContext) {
if (background != null) {
comboBoxField.GetFirstFormAnnotation().SetBackgroundColor(background.GetColor());
}
AbstractFormFieldRenderer.ApplyBorderProperty(this, comboBoxField.GetFirstFormAnnotation());
BorderStyleUtil.ApplyBorderProperty(this, comboBoxField.GetFirstFormAnnotation());
UnitValue fontSize = GetFontSize();
if (fontSize != null) {
comboBoxField.SetFontSize(fontSize.GetValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ You should have received a copy of the GNU Affero General Public License
using iText.Forms.Fields;
using iText.Forms.Form;
using iText.Forms.Form.Element;
using iText.Forms.Util;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
Expand Down Expand Up @@ -184,7 +185,7 @@ protected internal override void ApplyAcroField(DrawContext drawContext) {
choiceField.SetColor(color.GetColor());
}
choiceField.SetJustification(this.GetProperty<TextAlignment?>(Property.TEXT_ALIGNMENT));
AbstractFormFieldRenderer.ApplyBorderProperty(this, choiceField.GetFirstFormAnnotation());
BorderStyleUtil.ApplyBorderProperty(this, choiceField.GetFirstFormAnnotation());
Background background = this.GetProperty<Background>(Property.BACKGROUND);
if (background != null) {
choiceField.GetFirstFormAnnotation().SetBackgroundColor(background.GetColor());
Expand Down
Loading

0 comments on commit f2fcd67

Please sign in to comment.