如何使用 itextSharp 将多个图表 asp.net 导出到单个文档 PDF?
How to export multiple charts asp.net to a single document PDF using itextSharp?
我想使用 iTextSharp 将两个图表 Asp.net 导出到单个文档 PDF。
对于一张图表,我可以做到:
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();
}
但是,我无法同时导出两个图表...
有人有答案吗?非常感谢...
也许您引用了同一张图表,或者没有清理 MemoryStream
?这是一个生成两个不同图表的简单示例,然后将它们添加到 Document
.
首先是生成一些示例数据的辅助方法:
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();
}
}
然后添加图表:
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();
PDF 输出:
我想使用 iTextSharp 将两个图表 Asp.net 导出到单个文档 PDF。
对于一张图表,我可以做到:
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();
}
但是,我无法同时导出两个图表...
有人有答案吗?非常感谢...
也许您引用了同一张图表,或者没有清理 MemoryStream
?这是一个生成两个不同图表的简单示例,然后将它们添加到 Document
.
首先是生成一些示例数据的辅助方法:
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();
}
}
然后添加图表:
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();
PDF 输出: