- Create and Verify Digital Signatures using .NET Digital Signature Library
- .NET Digital Signature Library Code Samples
- Download .NET Digital Signature Library with all samples
static void BatchPdfSignature(string sourceFolder, string destFolder)
{
PdfSignature ps = new PdfSignature("");
//Load the certificate from Microsoft Store.
//The smart card or USB token certificates are usually available on Microsoft Certificate Store (start - run - certmgr.msc).
//If the smart card certificate not appears on Microsoft Certificate Store it cannot be used by the library
cs.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate(false, DigitalCertificateSearchCriteria.CommonNameCN, "Certificate Name");
//The smart card PIN dialog can be bypassed for some smart cards/USB Tokens.
//ATTENTION: This feature will NOT work for all available smart card/USB Tokens becauase of the drivers or other security measures.
DigitalCertificate.SmartCardPin = "123456";
System.IO.DirectoryInfo di;
System.IO.FileInfo[] rgFiles;
//get the pdf files from the folder
di = new System.IO.DirectoryInfo(sourceFolder);
//select only the PDF files
rgFiles = di.GetFiles("*.pdf");
Console.WriteLine("Number of PDF files: " + rgFiles.Length.ToString());
foreach (FileInfo fi in rgFiles)
{
//for readonly files
fi.Attributes = FileAttributes.Normal;
//load the PDF document
ps.LoadPdfDocument(sourceFolder + fi.Name);
//write the signed file
File.WriteAllBytes(destFolder + fi.Name, ps.ApplyDigitalSignature());
}
}
static void Main(string[] args)
{
//digitally sign all pdf files available on the source_folder
BatchPdfSignature("d:\\source_folder\\", "d:\\destination_folder\\");
}
See also: