Export WPF Chart to PDF in Console Application

We often receive queries from customers who are looking to use the WPF C1Chart control in a Console Application. They want to know how a Chart object can be implemented, customized and finally a PDF file can be generated with the Chart Image. This blog looks to cater to these requirements in a short and easy manner.

StartUp Requirements

Create a Console application and add references for the following WPF ComponentOne assemblies in the project.

  • C1.WPF.Pdf.4
  • C1.WPF.C1Chart.4
  • C1.WPF.C1Chart.Extended.4

You may refer to the given link to create a Console Application. http://msdn.microsoft.com/en-us/library/0wc2kk78%28v=vs.90%29.aspx Once we have the chart assemblies added to the project, we have to add the License entries. When building console applications, there is no form to add components and therefore Visual Studio won't create the Licenses.licx file. In these cases, create a temporary WPF Forms application and add the C1Chart component to XAML.Close the XAML window and copy the licenses.licx file into the console application project. Make sure the Licenses.licx file is configured as an embedded resource. To do this, right-click the Licenses.licx file in the Solution Explorer window and select Properties. In the Properties window, set the Build Action property to Embedded Resource.

Defining C1Chart through Code

Now you can define a C1Chart control as per the desired layout with the series and data. However, the important thing is that C1Chart control cannot be used directly. Since there is no Window to host the Chart, it needs a container. In this scenario Canvas object can be used. Check the code given below.

//Define Chart  
System.Windows.Controls.Canvas canvas = new System.Windows.Controls.Canvas();  
C1Chart Chart1;  

Chart1 = new C1Chart() { Height = 500, Width = 500 };  

DataSeries ds = new DataSeries();  
ds.ValuesSource = new int[] { 1, 2, 3, 4, 5 };  

ds = null;  
ds = new DataSeries();  
ds.ValuesSource = new int[] { 6, 7, 8, 9, 10 };  

Chart1.View.AxisX.Min = 0;  
Chart1.View.AxisX.Max = 20;  
Chart1.View.AxisX.MajorUnit = 5;  
Chart1.View.AxisX.AnnoAngle = 0;  
Chart1.ChartType = ChartType.BarStacked;  


canvas.Arrange(new System.Windows.Rect(0, 0, 500, 500));  

Customizing the Chart PlotElement

PlotElementLoaded event is commonly used to customize the C1Chart PlotElement. However, this event fires only when the Chart is rendered for display. With console application, we do not have this leverage and needs to retrieve the PlotElement directly from the collection. Following code snippet shows how this can be achieved.

//Customize Chart  

PlotElement ps = ((System.Windows.Controls.Panel)(Chart1.Data.Children[0])).Children[0] as PlotElement;  
ps.Stroke = new SolidColorBrush(Colors.Red);  
ps.Fill = new SolidColorBrush(Colors.Blue);  

ps.RenderTransform = new RotateTransform(200);  

It is important that Canvas layout is updated before customizing the Chart.

Exporting Chart Image to PDF

// Generate Chart Image  
MemoryStream ms = new MemoryStream();  
Chart1.SaveImage(ms, ImageFormat.Png);  
BitmapImage bi = new BitmapImage();  
bi.StreamSource = ms;  
WriteableBitmap wBmp = new WriteableBitmap(bi);  

// create and save pdf document  
C1PdfDocument pdf = new C1PdfDocument();  
pdf.DrawImage(wBmp, new Rect(0, 0, Chart1.ActualWidth, Chart1.ActualHeight));  
pdf.Save(@"<Path String>\\ConsoleChartImage.pdf");  

Combining the above code snippets, we can generate the following Chart image and save it to a PDF file. ConsoleChart Download the attached samples for complete implementation. Download Sample VB Download Sample C#


GrapeCity Developer Tools
comments powered by Disqus