Skip to content
Guides

Create Digital Signatures

Creating a digital signature involves two steps:

  1. Create a Signature Field

  2. Sign within the Signature Field

By following these two steps, you can either self-sign a document or invite others to sign within the signature field you've created.

Create a Signature Field

ComPDFKit offers support for customizing the styles of the signature form field and allows you to customize the appearance of your signature using drawn, image, and typed signatures.

This example shows how to create a signature field:

C#
// Create a Signature Field. 
// 
// Page Index: 0
// Rect: CRect (28, 420, 150, 370)
// Border RGB: { 0, 0, 0 }  
// Widget Background RGB: { 150, 180, 210 }
// 
CPDFPage page = document.PageAtIndex(0);
CPDFSignatureWidget signatureField = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
signatureField.SetRect(new CRect(28, 420, 150, 370)); 
signatureField.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
signatureField.SetWidgetBgRGBColor(new byte[] { 150, 180, 210 });
signatureField.UpdateAp();

Sign within the Signature Field

To sign within the signature field, you need to do three things:

  • Possess a certificate that conforms to the PKCS12 standard (in PFX or P12 format) and ensure that you know its password. You can create a compliant digital certificate using the built-in methods within the ComPDFKit PDF SDK.

  • Set the appearance of the digital signature.

  • Write the data into the signature field.

This example shows how to sign within the signature field:

C#
// Sign in the signature field.
//
// Text: Grantor Name
// Content:
// Name: Get the grantor's name from the certificate
// Date: Now (yyyy.mm.dd)
// Reason: I am the owner of the document
// DN: Subject
// IsContentAlginLeft: False
// IsDrawLogo: True
// LogoBitmap: logo.png
// text color RGB: { 0, 0, 0 }
// Output file name: document.FileName + "_Signed.pdf"
//
CPDFSignatureCertificate certificate = CPDFPKCS12CertHelper.GetCertificateWithPKCS12Path("Certificate.pfx", "ComPDFKit");
string name = GetGrantorFromDictionary(certificate.SubjectDict) + "\n";
string date = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss");
string reason = "I am the owner of the document.";
string location = certificate.SubjectDict["C"];
string DN = certificate.Subject;
CPDFSignatureWidget signatureField = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
CPDFSignatureConfig signatureConfig = new CPDFSignatureConfig
{
    Text = GetGrantorFromDictionary(certificate.SubjectDict),
    Content =
        "Name: " + name + "\n" +
        "Date: " + date + "\n" +
        "Reason: " + reason + " \n" +
        "Location: " + location + "\n" +
        "DN: " + DN + "\n",
    IsContentAlignLeft = false,
    IsDrawLogo = true,
    TextColor = new float[] { 0, 0, 0 },
    ContentColor = new float[] { 0, 0, 0 }
};

byte[] imageData = new byte[500 * 500];
signatureConfig.LogoData = imageData;
signatureConfig.LogoHeight = 500;
signatureConfig.LogoWidth = 500;

signatureField.UpdataApWithSignature(signatureConfig);
document.WriteSignatureToFilePath(signatureField,
    "filePath",
    "certificatePath", "password",
    location,
    reason,
    CPDFSignaturePermissions.CPDFSignaturePermissionsNone);


public static string GetGrantorFromDictionary(Dictionary<string, string> dictionary)
{
    string grantor = string.Empty;
    dictionary.TryGetValue("CN", out grantor);
    if (string.IsNullOrEmpty(grantor))
    {
        dictionary.TryGetValue("OU", out grantor);
    }
    if (string.IsNullOrEmpty(grantor))
    {
        dictionary.TryGetValue("O", out grantor);
    }
    if (string.IsNullOrEmpty(grantor))
    {
        grantor = "Unknown Signer";
    }
    return grantor;
}