Draw Signature TimeStamp in SignatureField like Adobe Acrobat

Hi,

how to add timestamp of signature to signature field while signing pdf?
I use the following code to sign pdf.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
using Syncfusion.Licensing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Security;

namespace WindowsFormsApp1
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // Application.Run(new Form1());

            SyncfusionLicenseProvider.RegisterLicense("......");

            var document = new PdfDocument();
            var page = document.Pages.Add();

            const string fileName = @"K:\ddd.pdf";

            var certificate = SelectSignerCertificate();
            var pdfCertificate = new PdfCertificate(certificate);

            var signature = new PdfSignature(document, page, pdfCertificate, "Signature")
            {
                Bounds = new RectangleF(10, 10, 200, 200)
            };

            var signatureGraphics = signature.Appearance.Normal.Graphics;
            var signRectangle = new RectangleF(2, 2, 100, 100);
            var pdfFormat = new PdfStringFormat();
            var f2 = new PdfTrueTypeFont(new Font("Times New Roman", 10f, FontStyle.Bold), true);
           
            // how to draw SignedDate ?
            signatureGraphics.DrawString(signature.SignedDate.ToString("yyyy-MM-dd HH:mm:ss \"GMT\"zzz"), f2, PdfBrushes.Black, signRectangle, pdfFormat);

            signature.Settings.CryptographicStandard = CryptographicStandard.CADES;
            signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA256;
            signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.AllowComments;

            signature.TimeStampServer = new TimeStampServer(new Uri("https://freetsa.org/tsr"));
            var externalSignature = new ExternalSigner(HashAlgorithmName.SHA256, certificate);
            var certificates = new List<X509Certificate2> { certificate };
            signature.AddExternalSigner(externalSignature, certificates, null);
            signature.CreateLongTermValidity(certificates);

            var stream = new MemoryStream();
            document.Save(stream);
            stream.Position = 0;
            document.Close(true);
           
            var bytes = stream.ToArray();
            File.Delete(fileName);
            File.WriteAllBytes(fileName, bytes);
        }

        private const string OidKeyUsage = "2.5.29.15";

        private static X509Certificate2 SelectSignerCertificate()
        {
            var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly | OpenFlags.IncludeArchived);

            var certificates = new X509Certificate2Collection();

            foreach (var certificate in store.Certificates)
            {
                if (certificate.HasPrivateKey && certificate.NotBefore <= DateTime.Now && certificate.NotAfter >= DateTime.Now)
                {
                    var validUsages = true;
                    foreach (var extension in certificate.Extensions)
                    {
                        if (string.Compare(extension.Oid.Value, OidKeyUsage, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            var keyUsage = new X509KeyUsageExtension();
                            keyUsage.CopyFrom(extension);
                            if ((keyUsage.KeyUsages & X509KeyUsageFlags.DigitalSignature) == 0 && (keyUsage.KeyUsages & X509KeyUsageFlags.NonRepudiation) == 0)
                            {
                                validUsages = false;
                            }
                            break;
                        }
                    }

                    if (validUsages)
                    {
                        certificates.Add(certificate);
                    }
                }
            }
            if (certificates.Count < 1)
                return null;

            certificates = X509Certificate2UI.SelectFromCollection(certificates, null, null, X509SelectionFlag.SingleSelection);
            if (certificates.Count < 1)
                return null;

            return certificates[0];
        }

    }

    public class ExternalSigner : IPdfExternalSigner
    {
        private readonly HashAlgorithmName _hashAlgorithmName;
        private readonly X509Certificate2 _certificate;

        public ExternalSigner(HashAlgorithmName hashAlgorithmName, X509Certificate2 certificate)
        {
            _hashAlgorithmName = hashAlgorithmName;
            _certificate = certificate;
        }

        public byte[] Sign(byte[] message, out byte[] timeStampResponse)
        {
            byte[] signedBytes = null;

            if (_certificate.PrivateKey is RSACryptoServiceProvider rsa)
            {
                signedBytes = rsa.SignData(message, HashAlgorithm);
            }

            timeStampResponse = null;
            return signedBytes;
        }

        public string HashAlgorithm => _hashAlgorithmName.Name;
    }
}


but signing takes place while saving document, so when reading SignedDate earlier it is not set.

Regards,

Piotr Aksamit 


6 Replies 1 reply marked as answer

JT Jeyalakshmi Thangamarippandian Syncfusion Team September 4, 2024 10:42 AM UTC

Hi Piotr Aksamit,

The signature will be signed only the call of document save, because of we could not sign the document before saving it and the signed date will be fetched at the time of saving the document. We have already logged this request "Support to set signed date in signature in pdf document", and we have considered and planned to implement this requirement in our upcoming 2024 Volume 3 Sp1 release. Once the feature is available, we will inform you. You can track the status of the feature using the following feedback link.

https://www.syncfusion.com/feedback/60188/support-to-set-signed-date-in-signature-in-pdf-document


Regards,

Jeyalakshmi T 



IJ Irfana Jaffer Sadhik Syncfusion Team November 21, 2024 12:51 PM UTC

Hi Piotr,

we regret not including the feature “support to set signed date in signature in pdf document” in our Volume 3 SP1, 2024 release. We have planned to include this feature in our upcoming weekly NuGet release which is expected on November 26th, 2024.
Please use the below feedback link to track the status of this feature.


Regards,

Irfana J.



IJ Irfana Jaffer Sadhik Syncfusion Team November 27, 2024 04:50 AM UTC

Hi Piotr,


we regret not including the feature “support to set signed date in signature in pdf document” in the last weekly release. We have planned to include this feature in our upcoming Volume 4 NuGet release scheduled for mid of the December without any further delay.
Please use the below feedback link to track the status of this feature.


Regards,

Irfana J.



IJ Irfana Jaffer Sadhik Syncfusion Team December 18, 2024 11:06 AM UTC

Hi Piotr,


The support for “Support to get and set the text alignment for PdfLoadedCombobox fields” has been included in our Essential Studio 2024 Volume 4 release v28.1.33 and We are excited to announce that our Essential Studio 2024 Volume 4 release (v28.1.33) has been rolled out


You can download our latest NuGet package using the link below:

NuGet Gallery | Syncfusion.Pdf.Net.Core 28.1.33

 Kindly refer the below Code sample for use this feature


FileStream stream = new FileStream("Form.pdf", FileMode.Open, FileAccess.Read);

PdfLoadedDocument document = new PdfLoadedDocument(stream);

PdfLoadedComboBoxField comboBox = document.Form.Fields[0] as  PdfLoadedComboBoxField;

comboBox.TextAlignment = PdfTextAlignment.Left;

document.Form.SetDefaultAppearance(true);

MemoryStream ms = new MemoryStream();  

document.Save(ms);



We sincerely thank you for your support and patience as we prepared this release. Please feel free to contact us if you need any further assistance.



Regards,

Irfana J




PA Piotr Aksamit April 22, 2025 08:46 PM UTC

Hi Irfana,

i was asking for support for

Support to set signed date in signature in pdf document, but your example is for

Support to get and set the text alignment for PdfLoadedCombobox fields

 

Can you please provide a sample, that will show how to draw signature date on signature image.


Regards,

Piotr Aksamit 




IJ Irfana Jaffer Sadhik Syncfusion Team April 23, 2025 07:33 AM UTC

Hi Piotr,


The support for Support to set signed date in signature in pdf document has been included in our Essential Studio 2024 Volume 4 release v28.1.33 and We are excited to announce that our Essential Studio 2024 Volume 4 release (v28.1.33) has been rolled out


You can download our latest NuGet package using the link below:

NuGet Gallery | Syncfusion.Pdf.Net.Core 28.1.33

 Kindly refer the below Code sample for use this feature:

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a new page to PDF document.
PdfPage page = document.Pages.Add();
//Create PDF Signature field.
PdfSignatureField signatureField = new PdfSignatureField(page, "Signature");
//Set properties to the signature field.
signatureField.Signature = new PdfSignature(page, "Signature", new DateTime(2019, 12, 24, 10, 50, 10));
signatureField.Bounds = new RectangleF(0, 400, 90, 20);
signatureField.ToolTip = "Signature";
//Add the form field to the document.
document.Form.Fields.Add(signatureField);
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the PDF document to stream.
document.Save(stream);
//Close the document.
document.Close(true);


Regards,

Irfana J.


Marked as answer
Loader.
Up arrow icon