I have been looking for a way to call the javascript function from my default.aspx.cs file...after reading some questions here, i discovered this approach(Call JavaScript function from C#) However I need to use the returned value from javascript function in my .net code.
I need to grab users input on canvas and save it to an image and append that image to a pdf file.
Here is the js function:
getSignatureImage: function () {
var tmpCanvas = document.createElement('canvas')
, tmpContext = null
, data = null
tmpCanvas.style.position = 'absolute'
tmpCanvas.style.top = '-999em'
tmpCanvas.width = element.width
tmpCanvas.height = element.height
document.body.appendChild(tmpCanvas)
if (!tmpCanvas.getContext && FlashCanvas)
FlashCanvas.initElement(tmpCanvas)
tmpContext = tmpCanvas.getContext('2d')
tmpContext.fillStyle = settings.bgColour
tmpContext.fillRect(0, 0, element.width, element.height)
tmpContext.lineWidth = settings.penWidth
tmpContext.strokeStyle = settings.penColour
drawSignature(output, tmpContext)
data = tmpCanvas.toDataURL.apply(tmpCanvas, arguments)
document.body.removeChild(tmpCanvas)
tmpCanvas = null
return data
}
Here is the pdf generating code in my default.aspx.cs:
document.SetMargins(20f, 20f, 85f, 20f);
long milliseconds2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
//Document document = new Document();
var output = new FileStream(Server.MapPath("~/PDFs/Test-File-" + milliseconds2 + ".pdf"), FileMode.Create);
pdfUrlLink = "Test-File-" + milliseconds2 + ".pdf";
var writer = PdfWriter.GetInstance(document, output);
// the image we're using for the page header
iTextSharp.text.Image imageHeader = iTextSharp.text.Image.GetInstance(Request.MapPath(
"~/Images/pdfHeader.jpg"
));
// instantiate the custom PdfPageEventHelper
MyPageEventHandler ex = new MyPageEventHandler()
{
ImageHeader = imageHeader
};
// and add it to the PdfWriter
writer.PageEvent = ex;
document.Open();
createContent();
document.Close();
submitEmail();
}
}
This is the modified code from other questions to integrate both world together:
string SignData = null;
Page page = HttpContext.Current.CurrentHandler as Page;
page.ClientScript.RegisterStartupScript(typeof(Page), "Test", "<script type='text/javascript'>" + SignData +"=getSignatureImage();</script>");
It doesn't work...Any help or input will be appreciated!