Display Binary Image on digital signature acrofield in PDF using C# and VB.Net in ASP.Net

Last Reply 2 months ago By pandeyism

Posted 2 months ago

I have a signature pad. I saved signature pad value as byte array image in database. I have displayed this image on image control. I wanted to display same image on Digital Signature control in Pdf.

To display content on pdf i have used class file in my project. Below i have attached code to display it

        public String Signature
        {
            get
            {
                //if employee has not agreed to the Electronic Signature form - do not place signature on form.
                EmployeePortal.Models.NCI.NCIEntities ent = EmployeePortal.Models.ClsNCIUtil.GetNCIEntity(custid);
                using (ent)
                {
                    EmployeePortal.Models.NCI.EmployeeSignature empSignature = ent.EmployeeSignatures.OrderByDescending(a => a.SignID).FirstOrDefault(a => a.EmployeeId == employee.EmployeeId);
                    EmployeePortal.Models.NCI.EmployeeOnboarding empsign = ent.EmployeeOnboardings.FirstOrDefault(a => a.EmployeeId == employee.EmployeeId);

                    if (empsign == null)
                    {
                        return null;
                    }
                    if (empsign.IAgree == true)
                    {
                        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                        //return encoding.GetString(empSignature.SignatureFile);
                        //SHA1 mySha1 = new SHA1CryptoServiceProvider();
                        //byte[] hash = mySha1.ComputeHash(empSignature.SignatureFile);
                        return encoding.GetString(empSignature.SignatureFile);
                    }
                    else
                    {
                        return null;
                    }
                } 
            }        
        }

Ho can i display image on digital signature acrofield?

Please help me out as soon as possible.

You are viewing reply posted by: pandeyism 2 months ago.
Posted 2 months ago Modified on 2 months ago

Hi JennyD6856,

Refer below sample.

HTML

<div>
    <div class="tools">
        <a href="#colors_sketch" data-tool="marker">Marker</a> <a href="#colors_sketch" data-tool="eraser">
            Eraser</a>
    </div>
    <br />
    <canvas id="colors_sketch" width="500" height="200"></canvas>
    <br />
    <br />
    <asp:HiddenField ID="hfImageData" runat="server" />
    <asp:Button ID="btnSave" Text="Save" runat="server" UseSubmitBehavior="false" OnClick="Save"
        OnClientClick="return ConvertToImage(this)" />
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/mobomo/sketch.js/master/lib/sketch.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $('#colors_sketch').sketch();
        $(".tools a").eq(0).attr("style", "color:#000");
        $(".tools a").click(function () {
            $(".tools a").removeAttr("style");
            $(this).attr("style", "color:#000");
        });
    });
    function ConvertToImage(btnSave) {
        var base64 = $('#colors_sketch')[0].toDataURL();
        $("[id*=hfImageData]").val(base64);
        __doPostBack(btnSave.name, "");
    };
</script>

Namespaces

C#

using iTextSharp.text;
using iTextSharp.text.pdf;

VB.Net

Imports iTextSharp.text
Imports iTextSharp.text.pdf

Code

C#

protected void Save(object sender, EventArgs e)
{
    string base64 = Request.Form[hfImageData.UniqueID].Split(',')[1];
    byte[] bytes = Convert.FromBase64String(base64);
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bytes);
    pdfDoc.Add(img);
    pdfDoc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=ImageExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
}

VB.Net

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Dim base64 As String = Request.Form(hfImageData.UniqueID).Split(","c)(1)
    Dim bytes As Byte() = Convert.FromBase64String(base64)
    Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
    Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bytes)
    pdfDoc.Add(img)
    pdfDoc.Close()
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=ImageExport.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.Write(pdfDoc)
    Response.End()
End Sub

Screenshot