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! Smile

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:

  1. Create new RadDocument instance.
  2. As per the elements hierarchy described in the documentation here, first you need to add a single Section to the document, and in the Section -- a single Paragraph.
  3. Now using the built-in export-to-image functionality in RadChart, export the chart contents into a memory stream and use it to create an ImageInline element that will hold the chart image in the resulting document.
  4. Add the ImageInline element to the Paragraph created earlier.
  5. That's it!

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!


About the Author

Manol Donev

Technical Lead,
WinCore Team

Related Posts

Comments

Comments are disabled in preview mode.