Edit this page

Export To PDF

This help topic will demonstrate how to export RadChart and RadGridView controls to PDF document simultaneously. For the purpose you should build the export-to-image functionality and export the image to PDF using our Documents Format Providers (also part of the RadControls for Silverlight suite).

A sample project demonstrating Exporting RadChart to PDF can be found in the end of this blogpost.

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. This way you can combine the output of several controls into a single document export. To export 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.

The following code creates the Chart part of the PDF document:

Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();
Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph();
BitmapImage bi = new BitmapImage();

using (MemoryStream ms = new MemoryStream())
{
    radChart.ExportToImage(ms, new PngBitmapEncoder());

    bi.BeginInit();
    bi.StreamSource = ms;
    bi.EndInit();

}

Telerik.Windows.Documents.Model.ImageInline image = new Telerik.Windows.Documents.Model.ImageInline(new WriteableBitmap(bi)) { Width = 700, Height = 500 };
   paragraph.Inlines.Add(image);
   section.Blocks.Add(paragraph);
   document.Sections.Add(section);
Dim section As New Telerik.Windows.Documents.Model.Section()
Dim paragraph As New Telerik.Windows.Documents.Model.Paragraph()
Dim bi As New BitmapImage()

Using ms As New MemoryStream()
    radChart.ExportToImage(ms, New PngBitmapEncoder())

    bi.BeginInit()
    bi.StreamSource = ms

    bi.EndInit()
End Using

Dim image As New Telerik.Windows.Documents.Model.ImageInline(New WriteableBitmap(bi)) With { _
    .Width = 700, _
    .Height = 500 _
}
paragraph.Inlines.Add(image)
section.Blocks.Add(paragraph)

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. We will not go into details as you may find them as well as the source listing in this online example and this blog post here.

Now that the document model is ready, you can add a Button that will function as exporter and call the PdfFormatProvider.Export(...) method from the Click event's body 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));

                    Telerik.Windows.Documents.FormatProviders.Pdf.PdfFormatProvider provider = 
                        new Telerik.Windows.Documents.FormatProviders.Pdf.PdfFormatProvider();                            

                    using (Stream output = dialog.OpenFile())
                    {
                        provider.Export(document, output);
                    }
                }
            }
        }
Private Sub Export_Click(sender As Object, e As System.Windows.RoutedEventArgs)
    Dim dialog As New SaveFileDialog()
    dialog.DefaultExt = ".pdf"
    dialog.Filter = "Adobe PDF Document (.pdf)|.pdf"

    If dialog.ShowDialog() = True Then
        If True Then
            Dim document As RadDocument = Me.CreateDocument()
            document.LayoutMode = DocumentLayoutMode.Paged
            document.Measure(RadDocument.MAX_DOCUMENT_SIZE)
            document.Arrange(New RectangleF(PointF.Empty, document.DesiredSize))

            Dim provider As New Telerik.Windows.Documents.FormatProviders.Pdf.PdfFormatProvider()

            Using output As Stream = dialog.OpenFile()
                provider.Export(document, output)
            End Using
        End If
    End If
End Sub