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