C#/VB.NET: Add or Delete Digital Signature in Excel

A digital signature is a type of electronic signature that can be used to verify the authenticity and integrity of digital documents. It can help recipients identify where the digital documents originate from and whether they have been changed by a third party after they were signed. In this article, we will demonstrate how to add or delete digital signatures in Excel in C# and VB.NET using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Add a Digital Signature to Excel in C# and VB.NET

You can add a digital signature to protect the integrity of an Excel file. Once the digital signature is added, the file becomes read-only to discourage further editing. If someone makes changes to the file, the digital signature will become invalid immediately.

Spire.XLS for .NET provides the AddDigitalSignature method of Workbook class to add digital signatures to an Excel file. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Initialize an instance of the X509Certificate2 class with the specified certificate (.pfx) file path and the password of the .pfx file.
  • Initialize an instance of the DateTime class.
  • Add a digital signature to the file using Workbook.AddDigitalSignature(X509Certificate2, string, DateTime) method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Core.MergeSpreadsheet.Interfaces;
using System;
using System.Security.Cryptography.X509Certificates;

namespace AddSignatureInExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Add digital signature to the file
            X509Certificate2 cert = new X509Certificate2("gary.pfx", "e-iceblue");
            DateTime certtime = new DateTime(2020, 7, 1, 7, 10, 36);
            IDigitalSignatures signature = workbook.AddDigitalSignature(cert, "e-iceblue", certtime);

            //Save the result file
            workbook.SaveToFile("AddDigitalSignature.xlsx", FileFormat.Version2013);
        }
    }
}

C#/VB.NET: Add or Delete Digital Signature in Excel

Delete All Digital Signatures from Excel in C# and VB.NET

Spire.XLS for .NET provides the RemoveAllDigitalSignatures method of Workbook class for developers to remove digital signatures from an Excel file. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Remove all digital signatures from the file using Workbook.RemoveAllDigitalSignatures() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace DeleteSignatureInExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("AddDigitalSignature.xlsx");

            //Remove all the digital signatures in the file
            workbook.RemoveAllDigitalSignatures();

            //Save the result file
            workbook.SaveToFile("RemoveDigitalSignature.xlsx", FileFormat.Version2013);
        }
    }
}

C#/VB.NET: Add or Delete Digital Signature in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.