Export MS Chart to PDF using Itextsharp in Asp.Net


Export MS Chart to PDF in Asp.Net

Using itextsharp, export ms chart into pdf file
I am rendering a chart using chart control in asp.Net 4.0. Now we want to export that chart image into pdf file. Interesting point is, I am not using template file here, I used Memory Mapped file feature though we can read/write file in memory.

Used tools and technology: .Net4.0, asp.net, c#, itextshartp, ms chart control

using (MemoryMappedFile mMappedFile = MemoryMappedFile.CreateNew("test.pdf", 1000000))
{
iTextSharp.text.Document doc =
new iTextSharp.text.Document(PageSize.A4, 72, 72, 172, 72);
using (MemoryMappedViewStream stream = mMappedFile.CreateViewStream())
{
// associate the document to the stream. PdfWriter.GetInstance(doc, stream);
//Save chart as image
 using (var chartimage = new MemoryStream())
 {
  //here Chart1 is chart control which rendered chart
  Chart1.SaveImage(chartimage, ChartImageFormat.Png);
  iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(chartim age.GetBuffer());
  img.ScalePercent(75f);
  doc.Open();
  doc.Add(new Paragraph(String.Format("Chart Title")));
  doc.Add(img); doc.Close();
 }
}
//read the data from the file and send it to the response stream
byte[] bytes;
using (MemoryMappedViewStream stream = mMappedFile.CreateViewStream())
{
  BinaryReader rdr = new BinaryReader(stream);
  bytes = new byte[mMappedFile.CreateViewStream().Length];
  rdr.Read(bytes, 0, (int)mMappedFile.CreateViewStream().Length);
}

Response.Clear();Response.ContentType = "Application/pdf";
Response.BinaryWrite(bytes);
Response.End();
}
Advertisements
Tagged with: , , , ,
Posted in .Net, Asp.Net, C#, ms Chart
3 comments on “Export MS Chart to PDF using Itextsharp in Asp.Net
  1. krishna says:

    i didn’t get any error but it’s not exported

    • pahamad says:

      I believe there is may be some error. Please debug the code and let me know at which live you are getting error.

  2. Devendra says:

    Yes, i also didn’t get any error but it’s not exported. i have debug the code as well but not not get any output.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Top Rated

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Blog Stats
  • 29,596 hits
%d bloggers like this: