none
Sign a pdf using usb token

    Question

  • I am using below code to sign a pdf using itextsharp version 5.5.1.0 . It works fine. But itextsharp 5.5.1.0 is having AGPL and i think it cannot be used in my application freely. But by using version below 4.1.6 which has GPL licence the same code does not work. So is there any alternative?

    using System;
        using System.Windows.Forms;
        using System.IO;
        
        using System.Security;
        using System.Security.Cryptography;
        using System.Security.Cryptography.X509Certificates;
        
        using iTextSharp.text.pdf;
        using iTextSharp.text.pdf.security;
        
        
        
        namespace SignPdf
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
                private SecureString GetSecurePin(string PinCode)
                {
                    SecureString pwd = new SecureString();
                    foreach (var c in PinCode.ToCharArray()) pwd.AppendChar(c);
                    return pwd;
                }
                
               
        
               
        
                private void SignWithThisCert(X509Certificate2 cert)
                {
                    string SourcePdfFileName = textBox1.Text;
                    string DestPdfFileName = textBox1.Text + "-Signed.pdf";
                    Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
                    Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData) };
                    IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA-1");
                    PdfReader pdfReader = new PdfReader(SourcePdfFileName);
                    FileStream signedPdf = new FileStream(DestPdfFileName, FileMode.Create);  //the output pdf file
                    PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');
                    PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
                    //here set signatureAppearance at your will
                    signatureAppearance.Reason = "Because I can";
                    signatureAppearance.Location = "My location";
                    signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
                    MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
                    //MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CADES);
                    MessageBox.Show("Done");
                }
        
                private void Form1_Load(object sender, EventArgs e)
                {
        
                }
        
              
                private void button1_Click_1(object sender, EventArgs e)
                {
                    //Sign with certificate selection in the windows certificate store
                    X509Store store = new X509Store(StoreLocation.CurrentUser);
                    store.Open(OpenFlags.ReadOnly);
                    X509Certificate2 cert = null;
                    //manually chose the certificate in the store
                    X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection);
                    if (sel.Count > 0)
                        cert = sel[0];
                    else
                    {
                        MessageBox.Show("Certificate not found");
                        return;
                    }
                    SignWithThisCert(cert);
                }
        
              
        
            }
        
        
        }

     

    • Moved by Caillen Thursday, June 12, 2014 4:30 AM
    Friday, May 30, 2014 1:04 PM

All replies

  • Hi IT researcher,

    Looks like it's a problem with the 3rd-party library iTextSharp. We don't provide support for 3rd-party projects. Please contact the support team of iTextSharp for help:

    http://itextpdf.com/support

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 02, 2014 7:51 AM
  • Ok. I understand.

    But is there any alternative way to sign a pdf using USB token?

    Monday, June 02, 2014 10:22 AM
  • hi.

    you may return to the iTextshap version to 5.3.3, it works fine to sign a PDF using external USB token.

    _______________________________________

    free C# Excel  component being evaluated, any other recommendation is appreciated.

    Thanks and Regards.




    • Edited by leo_456 Monday, June 09, 2014 9:03 AM
    Monday, June 09, 2014 8:56 AM
  • hi.

    you may return to the iTextshap version to 5.3.3, it works fine to sign a PDF using external USB token.

    _______________________________________

    free C# Excel  component being evaluated, any other recommendation is appreciated.

    Thanks and Regards.




    But version 5.3.3 is also having AGPL licence, As i said in question version below 4.1.6 has GPL licence.
    Monday, June 09, 2014 9:33 AM
  • Hi.. Please add the below line in your Code..

    signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(20, 10, 170, 1), 1,"Icsi-Vendor");

    I hope its work for you..

    Saturday, March 31, 2018 7:30 AM