You must Sign In to post a response.
  • Category: Windows 8

    Digital Signature in pdf using Itextsharp and also with Electronic Signature

    public static void signPdfFile(string sourceDocument, string destinationPath, Stream privateKeyStream, string keyPassword, string reason, string location)
    {
    Pkcs12Store pk12 = new Pkcs12Store(privateKeyStream, keyPassword.ToCharArray());
    privateKeyStream.Dispose();

    //then Iterate throught certificate entries to find the private key entry
    string alias = null;
    foreach (string tAlias in pk12.Aliases)
    {
    if (pk12.IsKeyEntry(tAlias))
    {
    alias = tAlias;
    break;
    }
    }
    var pk = pk12.GetKey(alias).Key;

    // reader and stamper
    PdfReader reader = new PdfReader(sourceDocument);
    using (FileStream fout = new FileStream(destinationPath, FileMode.Create, FileAccess.ReadWrite))
    {
    using (PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0'))
    {
    // appearance
    PdfSignatureAppearance appearance = stamper.SignatureAppearance;
    //appearance.Image = new iTextSharp.text.pdf.PdfImage();
    appearance.Reason = reason;
    appearance.Location = location;
    appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(20, 10, 170, 60), 1, "Icsi-Vendor");//.IsInvisible();//

    iTextSharp.text.Image watermark = iTextSharp.text.Image.GetInstance("D:\\src_pdf\\sign.jpg");
    appearance.Image = watermark;
    appearance.Image.ScaleToFit(70, 70);
    //appearance.Image.Alignment=100;
    appearance.Image.SetAbsolutePosition(100, 100);
    appearance.GetAppearance().AddImage(watermark);

    //digital signature
    IExternalSignature es = new PrivateKeySignature(pk, "SHA-256");
    MakeSignature.SignDetached(appearance, es, new Org.BouncyCastle.X509.X509Certificate[] { pk12.GetCertificate(alias).Certificate }, null, null, null, 0, CryptoStandard.CMS);

    stamper.Close();
    }
    }

    /* using (Stream inputPdfStream = new FileStream(sourceDocument, FileMode.Open, FileAccess.Read, FileShare.Read))
    using (Stream inputImageStream = new FileStream("D:\\src_pdf\\sign.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
    using (Stream outputPdfStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
    {
    var reader1 = new PdfReader(inputPdfStream);
    var stamper = new PdfStamper(reader1, outputPdfStream);
    var pdfContentByte = stamper.GetOverContent(1);

    Image image = Image.GetInstance(inputImageStream);
    image.SetAbsolutePosition(100, 100);
    pdfContentByte.AddImage(image);
    stamper.Close();
    }*/

    /* using (Stream input = new FileStream(destinationPath, FileMode.Open, FileAccess.Read, FileShare.Read))
    using (Stream output = new FileStream("D:\\tgt_pdf\\sample4.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
    {
    PdfReader reader1 = new PdfReader(input);
    PdfEncryptor.Encrypt(reader1, output, true, "secret", "secret", PdfWriter.ALLOW_PRINTING);
    }*/
    }
  • #710149
    Hi,
    There is a nice article explained on Digital signature in PDF using iTextSharp . Earlier we had used PdfSharp's API to sign and verify pdf documents. Now we can achieve this using iTextSharp.
    Check out the following article
    http://www.rahulsingla.com/blog/2012/09/digitally-sign-and-verify-pdf-documents-in-c-using-itextsharp-5-3-x-library

    Have a good Day

  • #710151
    this same thing how to do in Excel documents?

  • #765693
    download and try this high quality software for c# acrobat reader sign pdf on rasteredge, get the free trail and vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-edit-digital-signature/


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.