diff --git a/itext.tests/itext.sign.tests/itext/signatures/sign/PdfSignatureAppearanceTest.cs b/itext.tests/itext.sign.tests/itext/signatures/sign/PdfSignatureAppearanceTest.cs index e987008887..542178fdad 100644 --- a/itext.tests/itext.sign.tests/itext/signatures/sign/PdfSignatureAppearanceTest.cs +++ b/itext.tests/itext.sign.tests/itext/signatures/sign/PdfSignatureAppearanceTest.cs @@ -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; @@ -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); @@ -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); @@ -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 @@ -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 @@ -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 @@ -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); @@ -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); @@ -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))) { diff --git a/itext.tests/itext.sign.tests/resources/itext/signatures/sign/PdfSignatureAppearanceTest/cmp_signatureFieldAppearanceTest.pdf b/itext.tests/itext.sign.tests/resources/itext/signatures/sign/PdfSignatureAppearanceTest/cmp_signatureFieldAppearanceTest.pdf new file mode 100644 index 0000000000..3b4b50654a Binary files /dev/null and b/itext.tests/itext.sign.tests/resources/itext/signatures/sign/PdfSignatureAppearanceTest/cmp_signatureFieldAppearanceTest.pdf differ diff --git a/itext/itext.forms/itext/forms/form/renderer/AbstractFormFieldRenderer.cs b/itext/itext.forms/itext/forms/form/renderer/AbstractFormFieldRenderer.cs index 9111f9ca51..ebca19f677 100644 --- a/itext/itext.forms/itext/forms/form/renderer/AbstractFormFieldRenderer.cs +++ b/itext/itext.forms/itext/forms/form/renderer/AbstractFormFieldRenderer.cs @@ -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; @@ -300,64 +297,6 @@ internal virtual void ApplyProperties(IDictionary properties) { } } - /// Applies the border property. - /// the annotation to set border characteristics to. - internal virtual void ApplyBorderProperty(PdfFormAnnotation annotation) { - ApplyBorderProperty(this, annotation); - } - - /// Applies the border property to the renderer. - /// renderer to apply border properties to. - /// the annotation to set border characteristics to. - internal static void ApplyBorderProperty(IRenderer renderer, PdfFormAnnotation annotation) { - Border border = renderer.GetProperty(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(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(); diff --git a/itext/itext.forms/itext/forms/form/renderer/AbstractTextFieldRenderer.cs b/itext/itext.forms/itext/forms/form/renderer/AbstractTextFieldRenderer.cs index 92443b6db5..28ead4187c 100644 --- a/itext/itext.forms/itext/forms/form/renderer/AbstractTextFieldRenderer.cs +++ b/itext/itext.forms/itext/forms/form/renderer/AbstractTextFieldRenderer.cs @@ -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; @@ -74,7 +75,7 @@ internal virtual void ApplyDefaultFieldProperties(PdfFormField inputField) { inputField.SetColor(color.GetColor()); } inputField.SetJustification(this.GetProperty(Property.TEXT_ALIGNMENT)); - ApplyBorderProperty(inputField.GetFirstFormAnnotation()); + BorderStyleUtil.ApplyBorderProperty(this, inputField.GetFirstFormAnnotation()); Background background = this.GetProperty(Property.BACKGROUND); if (background != null) { inputField.GetFirstFormAnnotation().SetBackgroundColor(background.GetColor()); diff --git a/itext/itext.forms/itext/forms/form/renderer/CheckBoxRenderer.cs b/itext/itext.forms/itext/forms/form/renderer/CheckBoxRenderer.cs index 1c55bc398b..3d95148ad4 100644 --- a/itext/itext.forms/itext/forms/form/renderer/CheckBoxRenderer.cs +++ b/itext/itext.forms/itext/forms/form/renderer/CheckBoxRenderer.cs @@ -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; @@ -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(Property.BACKGROUND); if (background != null) { checkBox.GetFirstFormAnnotation().SetBackgroundColor(background.GetColor()); diff --git a/itext/itext.forms/itext/forms/form/renderer/RadioRenderer.cs b/itext/itext.forms/itext/forms/form/renderer/RadioRenderer.cs index eafff69200..60bb2e9c83 100644 --- a/itext/itext.forms/itext/forms/form/renderer/RadioRenderer.cs +++ b/itext/itext.forms/itext/forms/form/renderer/RadioRenderer.cs @@ -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(); diff --git a/itext/itext.forms/itext/forms/form/renderer/SelectFieldComboBoxRenderer.cs b/itext/itext.forms/itext/forms/form/renderer/SelectFieldComboBoxRenderer.cs index 1853dceb76..c903a8f869 100644 --- a/itext/itext.forms/itext/forms/form/renderer/SelectFieldComboBoxRenderer.cs +++ b/itext/itext.forms/itext/forms/form/renderer/SelectFieldComboBoxRenderer.cs @@ -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; @@ -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()); diff --git a/itext/itext.forms/itext/forms/form/renderer/SelectFieldListBoxRenderer.cs b/itext/itext.forms/itext/forms/form/renderer/SelectFieldListBoxRenderer.cs index 17fe00c95d..108740551a 100644 --- a/itext/itext.forms/itext/forms/form/renderer/SelectFieldListBoxRenderer.cs +++ b/itext/itext.forms/itext/forms/form/renderer/SelectFieldListBoxRenderer.cs @@ -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; @@ -184,7 +185,7 @@ protected internal override void ApplyAcroField(DrawContext drawContext) { choiceField.SetColor(color.GetColor()); } choiceField.SetJustification(this.GetProperty(Property.TEXT_ALIGNMENT)); - AbstractFormFieldRenderer.ApplyBorderProperty(this, choiceField.GetFirstFormAnnotation()); + BorderStyleUtil.ApplyBorderProperty(this, choiceField.GetFirstFormAnnotation()); Background background = this.GetProperty(Property.BACKGROUND); if (background != null) { choiceField.GetFirstFormAnnotation().SetBackgroundColor(background.GetColor()); diff --git a/itext/itext.forms/itext/forms/util/BorderStyleUtil.cs b/itext/itext.forms/itext/forms/util/BorderStyleUtil.cs new file mode 100644 index 0000000000..12403d087b --- /dev/null +++ b/itext/itext.forms/itext/forms/util/BorderStyleUtil.cs @@ -0,0 +1,93 @@ +/* +This file is part of the iText (R) project. +Copyright (c) 1998-2024 Apryse Group NV +Authors: Apryse Software. + +This program is offered under a commercial and under the AGPL license. +For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. + +AGPL licensing: +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +*/ +using iText.Forms.Fields; +using iText.Kernel.Pdf; +using iText.Kernel.Pdf.Annot; +using iText.Layout; +using iText.Layout.Borders; +using iText.Layout.Properties; + +namespace iText.Forms.Util { + /// This file is a helper class for the internal usage only. + /// + /// This file is a helper class for the internal usage only. + /// Be aware that its API and functionality may be changed in the future. + /// + public sealed class BorderStyleUtil { + /// Applies the border property to the annotation. + /// property container to apply border properties from. + /// the annotation to set border characteristics to. + public static void ApplyBorderProperty(IPropertyContainer container, PdfFormAnnotation annotation) { + Border border = container.GetProperty(Property.BORDER); + if (border == null) { + // For now, we set left border to an annotation, but appropriate borders for an element will be drawn. + border = container.GetProperty(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 BorderStyleUtil() { + } + // Private constructor. + } +} diff --git a/itext/itext.sign/itext/signatures/PdfSignatureAppearance.cs b/itext/itext.sign/itext/signatures/PdfSignatureAppearance.cs index 53f54cec26..58b9efa650 100644 --- a/itext/itext.sign/itext/signatures/PdfSignatureAppearance.cs +++ b/itext/itext.sign/itext/signatures/PdfSignatureAppearance.cs @@ -215,7 +215,7 @@ public virtual iText.Signatures.PdfSignatureAppearance SetPageRect(Rectangle pag /// /// /// layer 0. - [System.ObsoleteAttribute(@"will be deleted in the next major release. See iText.Forms.Fields.PdfSignatureFormField.SetBackgroundLayer(iText.Kernel.Pdf.Xobject.PdfFormXObject) ." + [System.ObsoleteAttribute(@"will be deleted in the next major release. See iText.Forms.Fields.PdfSignatureFormField.SetBackgroundLayer(iText.Kernel.Pdf.Xobject.PdfFormXObject) . Note that it should be called for the field retrieved with PdfSigner.GetSignatureField() method." )] public virtual PdfFormXObject GetLayer0() { if (n0 == null) { @@ -233,7 +233,7 @@ public virtual PdfFormXObject GetLayer0() { /// /// /// layer 2. - [System.ObsoleteAttribute(@"will be deleted in the next major release. See iText.Forms.Fields.PdfSignatureFormField.SetSignatureAppearanceLayer(iText.Kernel.Pdf.Xobject.PdfFormXObject) ." + [System.ObsoleteAttribute(@"will be deleted in the next major release. See iText.Forms.Fields.PdfSignatureFormField.SetSignatureAppearanceLayer(iText.Kernel.Pdf.Xobject.PdfFormXObject) . Note that it should be called for the field retrieved with PdfSigner.GetSignatureField() method." )] public virtual PdfFormXObject GetLayer2() { if (n2 == null) { @@ -393,7 +393,7 @@ public virtual iText.Signatures.PdfSignatureAppearance SetSignatureGraphic(Image /// Indicates that the existing appearances needs to be reused as a background layer. /// is an appearances reusing flag value to set. /// this instance to support fluent interface. - [System.ObsoleteAttribute(@"in favour of iText.Forms.Fields.PdfSignatureFormField.SetReuseAppearance(bool) ." + [System.ObsoleteAttribute(@"in favour of iText.Forms.Fields.PdfSignatureFormField.SetReuseAppearance(bool) . Note that it should be called for the field retrieved with PdfSigner.GetSignatureField() method." )] public virtual iText.Signatures.PdfSignatureAppearance SetReuseAppearance(bool reuseAppearance) { this.reuseAppearance = reuseAppearance; diff --git a/itext/itext.sign/itext/signatures/PdfSigner.cs b/itext/itext.sign/itext/signatures/PdfSigner.cs index 1fe4df4979..7026046a1f 100644 --- a/itext/itext.sign/itext/signatures/PdfSigner.cs +++ b/itext/itext.sign/itext/signatures/PdfSigner.cs @@ -30,12 +30,15 @@ You should have received a copy of the GNU Affero General Public License using iText.Forms; using iText.Forms.Fields; using iText.Forms.Form.Element; +using iText.Forms.Util; using iText.IO.Source; using iText.IO.Util; using iText.Kernel.Exceptions; +using iText.Kernel.Font; using iText.Kernel.Geom; using iText.Kernel.Pdf; using iText.Kernel.Pdf.Annot; +using iText.Layout.Properties; using iText.Pdfa; using iText.Signatures.Cms; using iText.Signatures.Exceptions; @@ -1062,7 +1065,7 @@ protected internal virtual PdfSigFieldLock CreateNewSignatureFormField(PdfAcroFo sigField.DisableFieldRegeneration(); sigField.SetReuseAppearance(appearance.IsReuseAppearance()).SetSignatureAppearanceLayer(appearance.GetSignatureAppearanceLayer ()).SetBackgroundLayer(appearance.GetBackgroundLayer()); - sigField.GetFirstFormAnnotation().SetFormFieldElement(appearance.GetSignatureAppearance()); + ApplyDefaultPropertiesForTheNewField(sigField); sigField.EnableFieldRegeneration(); acroForm.AddField(sigField, document.GetPage(pagen)); if (acroForm.GetPdfObject().IsIndirect()) { @@ -1318,7 +1321,6 @@ private bool IsDocumentPdf2() { private PdfSignature CreateSignatureDictionary(bool includeDate) { PdfSignature dic = new PdfSignature(); - PdfSignatureAppearance appearance = GetSignatureAppearance(); dic.SetReason(GetReason()); dic.SetLocation(GetLocation()); dic.SetSignatureCreator(GetSignatureCreator()); @@ -1330,6 +1332,32 @@ private PdfSignature CreateSignatureDictionary(bool includeDate) { return dic; } + private void ApplyDefaultPropertiesForTheNewField(PdfSignatureFormField sigField) { + SignatureFieldAppearance formFieldElement = appearance.GetSignatureAppearance(); + PdfFormAnnotation annotation = sigField.GetFirstFormAnnotation(); + annotation.SetFormFieldElement(formFieldElement); + // Apply default field properties: + sigField.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_NONE); + sigField.SetJustification(formFieldElement.GetProperty(Property.TEXT_ALIGNMENT)); + Object retrievedFont = formFieldElement.GetProperty(Property.FONT); + if (retrievedFont is PdfFont) { + sigField.SetFont((PdfFont)retrievedFont); + } + UnitValue fontSize = formFieldElement.GetProperty(Property.FONT_SIZE); + if (fontSize != null && fontSize.IsPointValue()) { + sigField.SetFontSize(fontSize.GetValue()); + } + TransparentColor color = formFieldElement.GetProperty(Property.FONT_COLOR); + if (color != null) { + sigField.SetColor(color.GetColor()); + } + BorderStyleUtil.ApplyBorderProperty(formFieldElement, annotation); + Background background = formFieldElement.GetProperty(Property.BACKGROUND); + if (background != null) { + sigField.GetFirstFormAnnotation().SetBackgroundColor(background.GetColor()); + } + } + /// An interface to retrieve the signature dictionary for modification. public interface ISignatureEvent { /// Allows modification of the signature dictionary. diff --git a/port-hash b/port-hash index ca523e832c..372f185e3a 100644 --- a/port-hash +++ b/port-hash @@ -1 +1 @@ -d89c0a8ccde6a80a3d53726a492d5bd17bdf386b +ecf88c810539f222870c3596432d09afa3911d4a