在WPF平台下,如何将C1Chart存成PDF文件呢?又如何将C1Chart打印出来呢?
本文就详细的介绍实现该功能的步骤。基本思路是将C1Chart存储成Image,然后画到C1PDFDocument里,并存成PDF文件,用C1ReportView打印这个PDF文件就可以了。
详细步骤如下:
1.C1Chart存储成图片
使用C1Chart.SaveImage方法将C1Chart存储成图片,方便以后的使用。
2.存成PDF文件
新建一个C1PDFdocument,使用C1PDFDocument.DrawImage方法将存好的C1Chart图片画到PDF里。
然后使用C1PDFDocument.Save方法将当前的文档存成PDF格式文件。
3.打印
拖拽一个c1ReportViewer1到Window,并使用c1ReportViewer1.LoadDocument方法导入已生成的含有C1Chart的PDF文件。
C1ReportView有内置的打印功能,至此,就可以对C1Chart进行打印了。
完成功能的C#代码参考:
// Generate Chart Image MemoryStream ms = new MemoryStream(); _c1chart.SaveImage(ms, ImageFormat.Png); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); WriteableBitmap wBmp = new WriteableBitmap(bi); // create and save pdf document C1PdfDocument pdf = new C1PdfDocument(); pdf.DrawImage(wBmp, new Rect(0, 0, _c1chart.ActualWidth, _c1chart.ActualHeight), ContentAlignment.TopCenter, Stretch.None); string fileName = "ChartImage.pdf"; pdf.Save(fileName); this.c1ReportViewer1.LoadDocument(fileName);
运行这个程序,在加载后会生成一个C1Chart图表,点击PrintChart按钮,就可以将图表展示在C1ReportView里,并且打印。如下图: