Print and Export

  • 4 minutes to read

You can print a chart and export it to multiple file formats.

ChartPrintingExporting_main

A project should reference the DevExpress.XtraPrinting.21.1 and DevExpress.Printing.v21.1.Core assemblies to execute print and export operations.

Use the following code to print a chart:

private void onButtonClick(object sender, EventArgs e) {
    if (chartControl.IsPrintingAvailable) {
        chartControl.Print();
    }
}

The table below lists related API members:

Member Description
ChartControl.Print Prints the chart.
ChartControl.IsPrintingAvailable Indicates whether the chart can be printed.

Print/Export a Chart from the Print Preview

You can display a Print Preview that contains the toolbox or the Ribbon.

Method Description
ChartControl.ShowPrintPreview Invokes the chart’s Print Preview that contains the toolbox.
ChartControl.ShowRibbonPrintPreview Invokes the chart’s Print Preview that contains the Ribbon.

The following code invokes the chart’s Print Preview that contains the Ribbon:

private void onButtonClick(object sender, RoutedEventArgs e) {
    chartControl.ShowRibbonPrintPreview(this);
}

Click the Quick Print item to print a chart:

Use the Print item to show the standard print dialog:

Select a file format in the Export drop-down list to export the chart:

Export a Chart from Code

The following methods export a chart to different formats:

Method Description
ChartControl.ExportToHtml Exports a chart to an HTML file.
ChartControl.ExportToImage Exports a chart to an image.
ChartControl.ExportToMht Exports a chart to an MHT file.
ChartControl.ExportToPdf Exports a chart to a PDF file.
ChartControl.ExportToXls Exports a chart to an XLS file.
ChartControl.ExportToXlsx Exports a chart to an XLSX file.
ChartControl.ExportToRtf Exports a chart to an RTF file.
ChartControl.ExportToDocx Exports a chart to a DOCX file.
ChartControl.ExportToSvg Exports a chart to an SVG image.

The following code exports a chart to a PDF file:

private void onButtonClick(object sender, EventArgs e) {
    // Exports a vector-based chart to a PDF file.
    chartControl.OptionsPrint.ImageFormat = DevExpress.XtraCharts.Printing.PrintImageFormat.Metafile;
    chartControl.ExportToPdf("D://document.pdf", 
                              new DevExpress.XtraPrinting.PdfExportOptions { ConvertImagesToJpeg = false });
}

Configure Print/Export Options

You can configure chart options before print or export. The following example prints the chart as a bitmap image:

private void onButtonClick(object sender, EventArgs e) {
    chartControl.OptionsPrint.SizeMode = PrintSizeMode.Stretch;
    chartControl.OptionsPrint.ImageFormat = PrintImageFormat.Bitmap;
    if (chartControl.IsPrintingAvailable) {
        chartControl.Print();
    }
}

The example above uses the following properties.

Property Description
ChartControl.OptionsPrint Contains print options.
ChartOptionsPrint.SizeMode Specifies how a chart is resized.
ChartOptionsPrint.ImageFormat Specifies an image format used to display the chart in the Print Preview.

Custom Print/Export

You can use a PrintableComponentLink object to customize the layout of a printed/exported document.

The following example shows the Print Preview of a landscape-orientated document that contains a chart that is stretched to fit the page:

using System.Drawing.Printing;
using DevExpress.XtraCharts;
using DevExpress.XtraPrinting;
//...
PrintableComponentLink pcl = new PrintableComponentLink(new PrintingSystem());
splineChart.OptionsPrint.SizeMode = DevExpress.XtraCharts.Printing.PrintSizeMode.Stretch;
pcl.Component = splineChart;
pcl.Landscape = true;
pcl.PaperKind = PaperKind.A4;
pcl.ShowPreviewDialog();

Related API members:

Property Description
ChartOptionsPrint.SizeMode Specifies how a chart is resized.
PrintableComponentLinkBase.Component Specifies the control that is printed.
LinkBase.Landscape Indicates whether the page orientation is landscape.
LinkBase.PaperKind Specifies the paper size.

See the following help topic for more information: How to: Use the PrintableComponentLink to Print DevExpress Controls.

Use an object of the CompositeLink class to print or export a chart with another control. Create PrintableComponentLink objects for each control and add them to the CompositeLinkBase.Links collection. The following example shows the Print Preview of a document that contains a chart and a grid:

using DevExpress.XtraCharts;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrintingLinks;
// ...
CompositeLink composLink = new CompositeLink(new PrintingSystem());
PrintableComponentLink pcLink1 = new PrintableComponentLink();
PrintableComponentLink pcLink2 = new PrintableComponentLink();
pcLink1.Component = this.chartControl1;
pcLink2.Component = this.gridControl1;
composLink.Links.Add(pcLink1);
composLink.Links.Add(pcLink2);
composLink.Landscape = true;
composLink.ShowPreview();

See also: How to: Combine Links via the CompositeLink.

See Also