This C# sample signs all the PDF files of a folder in one operation. A single PdfSignature object signs every file: the certificate and the settings are set once, and every file is loaded with LoadPdfDocument and signed with ApplyDigitalSignature.
How it works
- The signed files keep their names and they are written in another folder.
- A file that cannot be signed is reported and the batch continues, with a progress count.
- Add
TimeStamping.ServerUrlto time-stamp every signature.
C# code
/// <summary>
/// Gets the certificate used to sign the documents. A certificate can be loaded from various sources: choose one.
/// </summary>
static X509Certificate2 LoadSigningCertificate()
{
// 1. Load the certificate (with its private key) from a PFX or P12 file.
return DigitalCertificate.LoadCertificate(SampleFile("cert.pfx"), CertificatePassword);
// 2. Select a certificate from the Windows certificate store (Current User). Smart cards and USB tokens
// usually publish their certificates in this store (start - run - certmgr.msc). If the certificate of a
// smart card is not visible in the Windows store, the library cannot use it with this method.
// The user selects the certificate from a dialog window:
// return DigitalCertificate.LoadCertificate(false, string.Empty, "Select Certificate", "Select the certificate for digital signature");
// A batch signature without user intervention (no selection window) with a certificate from the Windows
// store is shown in the sample "Batch PDF Signature Without User Intervention".
}
/// <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)
{
// A single PdfSignature object signs all the files: the certificate and the settings are set only once.
PdfSignature pdfSignature = new PdfSignature(LibrarySerialNumber);
pdfSignature.DigitalSignatureCertificate = LoadSigningCertificate();
pdfSignature.SigningReason = "I approve this document";
// 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;
int processedFiles = 0;
foreach (string pdfFile in pdfFiles)
{
try
{
// Load the PDF document of the current file.
pdfSignature.LoadPdfDocument(pdfFile);
// Write the signed file.
File.WriteAllBytes(Path.Combine(destinationFolder, Path.GetFileName(pdfFile)), pdfSignature.ApplyDigitalSignature());
signedFiles++;
Console.WriteLine("File " + Path.GetFileName(pdfFile) + " was signed in PDF format (" + (processedFiles + 1) + " of " + pdfFiles.Length + ").");
}
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);
}
processedFiles++;
}
Console.WriteLine("Signed PDF files: " + signedFiles + " of " + pdfFiles.Length + Environment.NewLine);
}
The same project signs the files of a folder in CAdES format and in parallel: see Batch CAdES digital signature. To sign without any window, with a smart card or a token, see Sign PDF files without user intervention.
Run the full sample. The complete Visual Studio project Batch P7M P7S CAdES and PDF Signatures 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
How fast is batch PDF signing?
With a PFX certificate the signature itself takes milliseconds; the time depends on the size of the documents and on the time-stamping server. A smart card or a token is slower, because every signature is computed by the device. Several files can be signed in parallel with a PFX certificate.
Is there a ready-made program for batch signing?
Yes. PDF Signer signs a whole folder of PDF documents from a Windows application, without programming.