Services, scheduled tasks and scripts cannot click a certificate selection window or type a PIN. This C# sample signs all the PDF files of a folder with a certificate taken from the Windows certificate store, found by its common name, without any dialog window.
How it works
DigitalCertificate.LoadCertificate(false, DigitalCertificateSearchCriteria.CommonNameCN, name)returns the first certificate of the store “Current User – Personal” that matches; it can also search by serial number, thumbprint, organization or e-mail, and in the “Local Machine” store (for services).- For a smart card or a USB token,
DigitalCertificate.SmartCardPingives the PIN to the driver, so the PIN window does not appear. It works with the drivers that accept the PIN from the application, and it is used for a single signature: set it before every signature, on the thread that signs. - A file that cannot be signed (for example a damaged PDF) is reported and the batch continues.
C# code
/// <summary>
/// Gets, without user intervention, a certificate from the Windows certificate store.
/// </summary>
static X509Certificate2 LoadCertificateFromWindowsStore()
{
// The first certificate of the store "Current User - Personal" that matches the criterion is returned.
// The parameters: only the valid certificates (false = all of them), the search criterion (the common name,
// but it can also be the serial number, the thumbprint, the organization, the e-mail, etc.) and the searched value.
X509Certificate2 certificate = DigitalCertificate.LoadCertificate(false, DigitalCertificateSearchCriteria.CommonNameCN, CertificateCommonName);
// Other overloads: the store "Local Machine" instead of "Current User":
// DigitalCertificate.LoadCertificate(false, DigitalCertificateSearchCriteria.CommonNameCN, CertificateCommonName, true);
// The PIN of a smart card or of a USB token: it avoids the PIN window, but it does not work for all
// the smart cards and USB tokens (it depends on their drivers). The PIN is used for a single signing
// operation: set it before every signature, on the thread that signs.
// DigitalCertificate.SmartCardPin = "1234";
// The PIN can also be set by the overload of the method: LoadCertificate(false, criterion, value, "1234").
if (certificate == null)
throw new Exception("The certificate '" + CertificateCommonName + "' was not found in the Windows certificate store. " +
"Install the file cert.pfx (password 123456) in the store 'Current User - Personal'.");
return certificate;
}
/// <summary>
/// Signs all the PDF files of a folder. The signed files keep their names and they are saved in another folder.
/// </summary>
static void SignAllPdfFiles(string sourceFolder, string destinationFolder)
{
PdfSignature pdfSignature = new PdfSignature(LibrarySerialNumber);
pdfSignature.DigitalSignatureCertificate = LoadCertificateFromWindowsStore();
// Optionally, the signatures can be time-stamped (RFC 3161) by a Time Stamping Authority (TSA).
// pdfSignature.TimeStamping.ServerUrl = new Uri("https://ca.signfiles.com/TSAServer.aspx");
string[] pdfFiles = Directory.GetFiles(sourceFolder, "*.pdf");
Console.WriteLine("Number of files that will be signed in PDF format: " + pdfFiles.Length);
int signedFiles = 0;
foreach (string pdfFile in pdfFiles)
{
try
{
pdfSignature.LoadPdfDocument(pdfFile);
File.WriteAllBytes(Path.Combine(destinationFolder, Path.GetFileName(pdfFile)), pdfSignature.ApplyDigitalSignature());
signedFiles++;
Console.WriteLine("File " + Path.GetFileName(pdfFile) + " was signed in PDF format.");
}
catch (Exception ex)
{
// A file that cannot be signed (e.g. a damaged PDF) does not stop the batch.
Console.WriteLine("File " + Path.GetFileName(pdfFile) + " was NOT signed: " + ex.Message);
}
}
Console.WriteLine("Signed PDF files: " + signedFiles + " of " + pdfFiles.Length + Environment.NewLine);
}
Run the full sample. The complete Visual Studio project Batch PDF Signature Without User Intervention is included in the library download, in the folder NET Sample Projects (.NET 8, .NET 9, .NET 10 and .NET 11) and in NET Framework Sample Projects (.NET Framework 4.6.2), with the test files and a test certificate. Without a serial number the library runs in demo mode. Download the library and the samples · User manual
Frequently asked questions
Can I bypass the PIN window of my USB token?
For many smart cards and USB tokens, yes: set DigitalCertificate.SmartCardPin before every signature. It depends on the driver of the device; some drivers always show their own PIN window.
Can a Windows service use the certificate?
Yes. Install the certificate in the “Local Machine” store, give the account of the service access to its private key, and use the overload of LoadCertificate that searches the Local Machine store.