Reputation: 89
I want to export two Charts Asp.net to a single document PDF using iTextSharp.
For one chart, I could do it :
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
using (MemoryStream stream = new MemoryStream())
{
Chart1.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScalePercent(75f);
pdfDoc.Add(chartImage);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Charts.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
BUT, I can't export two Charts in the same time...
Someone has the answer ? thank you very much...
Upvotes: 0
Views: 2972
Reputation: 9372
Perhaps you're referencing the same chart, or not cleaning up the MemoryStream
? Here's a simple example that generates two different charts, and then adds them to the Document
.
First a helper method to generate some sample data:
byte[] GetChartImage(params int[] points)
{
using (var stream = new MemoryStream())
{
using (var chart = new Chart())
{
chart.ChartAreas.Add(new ChartArea());
Series s = new Series();
for (int i = 0; i < points.Length; ++i)
{
s.Points.AddXY(points[i], points[i]);
}
chart.Series.Add(s);
chart.SaveImage(stream, ChartImageFormat.Png);
}
return stream.ToArray();
}
}
Then add the charts:
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=charts.pdf");
using (Document document = new Document())
{
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
document.Add(Image.GetInstance(GetChartImage(3, 5, 7)));
document.Add(Image.GetInstance(GetChartImage(2, 4, 6, 8)));
}
Response.End();
The PDF output:
Upvotes: 1