Search code examples
javascriptasp.netpdfitextactivexobject

How to make downloadable correct pdf file and verify it in itextsharp in asp.net?


I've created a PDF file using itextsharp which is working fine. I've created an Activex control to digitally sign PDF file using USB token at client side. The client digitally signs the PDF file using its USB token and the PDF file stores onto server and the others authorized person can download it. Activex simply takes a PDF file and digitally signs and temporarily stores the PDF file and then returns the PDF file as base64 string using PdfEncodings class of itextsharp library. To use the Activex object, I've created two asp.net hidden fields one for unsigned and another one for signed PDF. After creating a PDF file, I convert it to base64 string and then assign it to asp.net hidden field and then call the javascript function to sign using Activex control and then assign the value to another asp.net hidden field and then take that value and make it downloadable but, when the PDF gets downloaded, it says PDF is corrupted. I've checked the PDF file in temporary folder which is correct.

Code for digitally sign pdf in activex control

PdfReader pdfReader = new PdfReader(PdfEncodings.ConvertToBytes(unsignedPDF, null));
        string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";
        FileStream signedPdf = new FileStream(fileName, FileMode.Create);
        PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');
        X509Store store = new X509Store(StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
        X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection);
        if (sel.Count > 0)
        {
            X509Certificate2 cert = sel[0];
            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");
            PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
            signatureAppearance.Reason = "I declare this document is legal";
            signatureAppearance.Location = "Delhi";
            signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), pdfReader.NumberOfPages, "Signature");
            signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
            MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
            return PdfEncodings.ConvertToString(File.ReadAllBytes(fileName), null);
        }
        else
        {
            return "ERROR";
        }

Code for downloading PDF from browser:

byte[] pdfByte = PdfEncodings.ConvertToBytes(signed.Value, null);
        Response.Clear();
        MemoryStream ms = new MemoryStream(pdfByte);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
        Response.Buffer = true;
        ms.WriteTo(Response.OutputStream);
        Response.End();

Solution

  • Pdfencodings is not meant for that, I just used convert.tobase64string and convert.frombase64string and it worked. :)