Every now and then when discussing the RadChart export options with customers, we receive the inquiry whether it is possible to export the control to PDF as well. The answer is – YES, it is definitely possible!
While the chart control itself does not provide this feature out-of-the-box, you can easily build upon its export-to-image functionality and export it to PDF using our Documents Format Providers (also part of the RadControls for Silverlight suite).
Basically, all you need to do is construct a document model based on the contents you want to export, and then pass the document to the PdfFormatProvider. Note that in this way you can combine the output of several controls into a single document export. Let’s say that we have a Silverlight report page that visualizes the data in RadChart and RadGridView components and we want to export both of them into the same PDF document.
About the chart part of the document:
Here is the sample code snippet as well:
Section section = new Section();
Paragraph paragraph = new Paragraph();
BitmapImage bi = new BitmapImage();
using (MemoryStream ms = new MemoryStream())
{
RadChart1.ExportToImage(ms, new PngBitmapEncoder());
bi.SetSource(ms);
}
ImageInline image = new ImageInline(new WriteableBitmap(bi)) { Width = 700, Height = 500 };
paragraph.Inlines.Add(image);
section.Blocks.Add(paragraph);
document.Sections.Add(section);
As for the grid part of the constructed document – all you need to do is create a second Section element and construct Table with the respective TableRow / TableCell elements. I will not go into the details and the source listing here as they are already available in this online example and this blog post here.
Now that the document model is ready, all you need to do is call the PdfFormatProvider.Export(...) method like this:
private void Export_Click(object sender, System.Windows.RoutedEventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "*.pdf";
dialog.Filter = "Adobe PDF Document (*.pdf)|*.pdf";
if (dialog.ShowDialog() == true)
{
{
RadDocument document = this.CreateDocument();
document.LayoutMode = DocumentLayoutMode.Paged;
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
PdfFormatProvider provider = new PdfFormatProvider();
using (Stream output = dialog.OpenFile())
{
provider.Export(document, output);
}
}
}
}
You can find attached the sample application and a sample pdf export as well.
Enjoy!