Hi,
I have developed a form to display google chart and now i want export to PDF
How to this. I am getting error as like 'Object reference not set to an instance of an object.'
with follwing line.
byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]); because of the
string base64 = Request.Form["chart_data"]; is getting null value..
asp:Content ID="Content1" ContentPlaceHolderID="cHead" runat="Server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
title: 'USA City Distribution'
};
var json = '[["ShipCity", "TotalOrders"], ["Albuquerque", 18], ["Anchorage", 10], ';
json += '["Boise", 31], ["Butte", 3], ["Elgin", 5], ["Eugene", 11], ["Kirkland", 3], ';
json += '["Lander", 9], ["Portland", 12], ["San Francisco", 4], ["Seattle", 14], ["Walla Walla", 2]]';
var data = google.visualization.arrayToDataTable($.parseJSON(json));
var chart = new google.visualization.PieChart($("#chart")[0]);
chart.draw(data, options);
var image = '<img src="' + chart.getImageURI() + '">';
$("[id*=btnExport]").click(function () {
$("input[name=chart_data]").val(chart.getImageURI());
});
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mBody" runat="Server">
<div id="chart" style="width: 500px; height: 300px;">
</div>
<br />
<asp:Button ID="btnExport" Text="Export" runat="server" OnClick="Export_Click" />
</asp:Content>
protected void Export_Click(object sender, EventArgs e)
{
string base64 = Request.Form["chart_data"];
byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]);
string fileName = "Chart.pdf";
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(bytes);
using (MemoryStream memoryStream = new MemoryStream())
{
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
document.Add(image);
document.Close();
bytes = memoryStream.ToArray();
memoryStream.Close();
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}