Skip to content

Latest commit

 

History

History
183 lines (119 loc) · 7.58 KB

common-export-support.md

File metadata and controls

183 lines (119 loc) · 7.58 KB
title page_title description slug tags published position
Export Support
Export Support
Export Support
common-export-support
export,support
true
11

Export Support

More often than not, it is a good idea to be able to preserve the data that the different controls show and edit during the life cycle of the application even after the application is closed. There are different ways to save this information and various approaches can be adopted depending on the type of the content.

Built-In Export

Several of the controls in the Telerik UI for WPF suite come with built-in export capabilities. Among those are RadDiagram, RadGridView and RadRichTextBox.

To learn more about these abilities take a look at the Export article in the desired control's documentation.

Document Processing Integration

The Telerik UI for WPF suite contains document libraries specifically designed for import, export and document editing.

  • [RadPdfProcessing]({%slug radpdfprocessing-overview%}): Supports export to PDF.
  • [RadSpreadProcessing]({%slug radspreadprocessing-overview%}): Supports export to XLSX, CSV, PDF and plain text (TXT). {% if site.site_name == 'WPF' %}
  • [RadWordsProcessing]({%slug radwordsprocessing-overview%}): Supports export to DOCX, RTF, HTML, PDF and plain text(TXT). {%endif%}

The libraries give you the ability to create a document from scratch and export it to its supported file formats. This means you can export practically any control either by exporting it to an image and adding the image to the resulting document or by creating an appropriate for the context structure (for example, a table when exporting RadGridView to DOCX).

There are several controls that already provide sample code which you can use as the base of export functionality, you could take a look at them in the Telerik XAML SDK repository:

Export XAML UI Elements to PDF

The API of RadPdfProcessing is designed to resemble XAML and this allows you easy conversion of UI elements to PDF by converting any XAML primitive to a PDF instruction. For base of such conversion you can use the RadPdfProcessing Export UI Element to PDF example which demonstrates how to export several of the controls in the Telerik UI for WPF suite, including a combination of several controls in the same view.

Common Export Support

The code operates with a set of renderers deriving from the base UIElementRendererBase - TextBlockRenderer, BorderRenderer, etc. This allows separation, since each concrete render is responsible for drawing the element it is intended for without dependencies to the other renderers, and gives you the ability to extend the sample code to fit your precise needs if you need to.

Take a look at the source code of the example on GitHub and the documentation of the relevant [FixedDocumentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) class.

Export Images With ExportExtensions

Some controls can be exported directly using the ExportExtensions class which is part of the Telerik.Windows.Controls assembly. It allows you to export in several image formats listed below:

Image formats

  • Png: Portable Netwok Graphic. Use ExportToImage(FrameworkElement, Stream) method.

  • Bmp: Bitmap file. Use ExportToImage(FrameworkElement, Stream, BitmapEncoder) where the encoder is of type BmpBitmapEncoder.

  • Xps: XML Paper Specification file. Use ExportToXpsImage(FrameworkElement, Stream) method to export content as an XPS image.

This approach is convenient for controls which have a size that allows direct export on one page, such as a RadGauge for example.

{% if site.site_name == 'WPF' %}

There are overloads for the methods listed above, which take as parameter the name of the file you want to export to, instead of a stream. This allows you to easily export your control directly to the file system. {% endif %}

Example 1 demonstrates how to export RadGauge to PNG file format. The physical path to the image is provided run-time via SaveFileDialog:

{% if site.site_name == 'Silverlight' %}

[C#] Example 1: Export Control to PNG

{{region common-export-support_0}} private void Button_Click(object sender, RoutedEventArgs e) { string extension = "png"; SaveFileDialog dialog = new SaveFileDialog() { DefaultExt = extension, Filter = "Png (.png)|.png" };

	if (dialog.ShowDialog() == true)
	{
		using (Stream stream = dialog.OpenFile())
		{
			Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(
				 this.radGauge, stream, new Telerik.Windows.Media.Imaging.PngBitmapEncoder());
		}
	}
}

{{endregion}}

[VB.NET] Example 1: Export Control to PNG

{{region common-export-support_1}} Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Dim extension As String = "png" Dim dialog As New SaveFileDialog() With {.DefaultExt = extension, .Filter = "Png (.png)|.png"}

	If dialog.ShowDialog() = True Then
		Using stream As Stream = dialog.OpenFile()
			Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(Me.radGauge, stream, New Telerik.Windows.Media.Imaging.PngBitmapEncoder())
		End Using
	End If
 End Sub

{{endregion}} {% endif %}

{% if site.site_name == 'WPF' %}

[C#] Example 1: Export Control to PNG

{{region common-export-support_2}} private void Button_Click(object sender, RoutedEventArgs e) { string extension = "png"; SaveFileDialog dialog = new SaveFileDialog() { DefaultExt = extension, Filter = "Png (.png)|.png" };

	if (dialog.ShowDialog() == true)
	{
		using (Stream stream = dialog.OpenFile())
		{
			Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(
				 this.radGauge, stream, new System.Windows.Media.Imaging.PngBitmapEncoder());
		}
	}
}

{{endregion}}

[VB.NET] Example 1: Export Control to PNG

{{region common-export-support_0}} Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Dim extension As String = "png" Dim dialog As New SaveFileDialog() With {.DefaultExt = extension, .Filter = "Png (.png)|.png"}

	If dialog.ShowDialog() = True Then
		Using stream As Stream = dialog.OpenFile()
			Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(Me.radGauge, stream, New System.Windows.Media.Imaging.PngBitmapEncoder())
		End Using
	End If
 End Sub

{{endregion}} {% endif %}

tipExporting a control to an image requires that the control is measured and arranged. Otherwise, unexpected results may occur.

See Also

  • [RadPdfProcessing]({%slug radpdfprocessing-overview%})

  • [RadSpreadProcessing]({%slug radspreadprocessing-overview%})

{% if site.site_name == 'WPF' %}

  • [RadWordsProcessing]({%slug radwordsprocessing-overview%}) {%endif%}

  • [RadGridView Export]({%slug gridview-export%})

  • Export UI Element to PDF