Digital signature of PDF file by using DSC Token C# with Itextsharp
Digital signature of PDF file by using DSC Token C# with Itextsharp
Required
You will have to digital signature token issued by government.
Also you know your DSC Password.
First Install DSC token device on your system after installing you can check by
Internet Option ->Content ->Click on Certificates here you will get certificate Details.
My code will read PDF File from folder and apply digital signature on PDF File all pages and signed PDF File will save on another Folder.
Here below code complete code in console application using c#
First add Itextsharp.dll file from new get package manager and add reference below
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.security;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System.Security.Cryptography.X509Certificates;
using System.Security;
class DigitalWithoutPassword
{
static string subj = "";
static void Main(string[] args)
{
int num = 0;
string text = "";
string text2 = "";
int num2 = 0;
string FilePath = @"E:/DigitalSignature/invoice/";
string SignFilePath = @"E:/DigitalSignature/Signinvoice/";
string[] files = Directory.GetFiles(FilePath);
DirectoryInfo dir = new DirectoryInfo(FilePath);
foreach (FileInfo flInfo in dir.GetFiles())
{
string text3 = "", SignPdfName = "";
text3 = flInfo.Name;
string path = "OldFilePath:" + flInfo.DirectoryName + "\\" + text3 + "$";
SignPdfName = "NewFilePath:" + SignFilePath + text3 + "$";
text = getBetween(path, "OldFilePath:", "$");
text2 = getBetween(SignPdfName, "NewFilePath:", "$");
}
Console.WriteLine("Reading File Path");
// streamReader.Close();
Console.WriteLine(text+" "+ text2);
try
{
Console.WriteLine("Searching for certificate");
X509CertificateParser x509CertificateParser = new X509CertificateParser();
X509Certificate2 certificate = null;
X509Store x509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
x509Store.Open(OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = x509Store.Certificates;
x509Store.Open(OpenFlags.MaxAllowed);
X509Certificate2Collection x509Certificate2Collection = X509Certificate2UI.SelectFromCollection(x509Store.Certificates, "Please choose certificate:", "", X509SelectionFlag.SingleSelection);
if (x509Certificate2Collection.Count > 0)
{
certificate = x509Certificate2Collection[0];
subj = certificate.Subject;
}
x509Store.Close();
IList<Org.BouncyCastle.X509.X509Certificate> list = new List<Org.BouncyCastle.X509.X509Certificate>();
X509Chain x509Chain = new X509Chain();
x509Chain.Build(certificate);
X509ChainElementEnumerator enumerator = x509Chain.ChainElements.GetEnumerator();
while (enumerator.MoveNext())
{
X509ChainElement current = enumerator.Current;
list.Add(DotNetUtilities.FromX509Certificate(current.Certificate));
}
if (File.Exists(text))
{
}
PdfReader pdfReader = new PdfReader(text);
num2 = pdfReader.NumberOfPages;
pdfReader.Close();
Console.WriteLine("Signing file...");
for (int i = 1; i <= num2; i++)
{
if (i != 1)
{
File.Delete(text);
File.Copy(text2, text);
File.Delete(text2);
}
PdfReader pdfReader2 = new PdfReader(text);
FileStream fileStream = new FileStream(text2, FileMode.Create);
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader2, fileStream, '\0', Path.GetTempFileName(), append: true);
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
signatureAppearance.Acro6Layers = false;
signatureAppearance.Reason = "Digitily Sign Invoice";
string[] sub = subj.Split(',');
string Loc = sub[2].ToString();
string[] Sign = Loc.Split(new char[] { '=' }, 2);
string Location = Sign[1].ToString();
signatureAppearance.Location = Location;
signatureAppearance.SignDate = DateTime.Now;
signatureAppearance.SetVisibleSignature(new Rectangle(600f, 55f, 450f, 90f), i, null);
IExternalSignature externalSignature = new X509Certificate2Signature(certificate, "SHA-1");
MakeSignature.SignDetached(signatureAppearance, externalSignature, list, null, null, null, 0, CryptoStandard.CMS);
fileStream.Close();
pdfReader2.Close();
pdfStamper.Close();
}
File.Delete(text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
// CertSelect
public static string getBetween(string strSource, string strStart, string strEnd)
{
if (strSource.Contains(strStart) && strSource.Contains(strEnd))
{
int num = strSource.IndexOf(strStart, 0) + strStart.Length;
int num2 = strSource.IndexOf(strEnd, num);
return strSource.Substring(num, num2 - num);
}
return "";
}
}