ChartControl.ExportToPdf(String) Method

Exports the chart’s layout to the specified PDF file.

Namespace: DevExpress.XtraCharts

Assembly: DevExpress.XtraCharts.v21.1.UI.dll

Declaration

public void ExportToPdf(
    string filePath
)

Parameters

Name Type Description
filePath String

A String specifying the full path (including the file name and extension) where the PDF file will be created.

Remarks

If a file under the specified file path exists, the new file replaces it.

To show the standard Print dialog, use the ChartControl.Print method. The dialog allows end-users to print the chart, select the printer (if required), specify the range of pages to print, the number of copies, etc.

To display the DevExpress Print Preview use one of the following methods.

Method Description
ChartControl.ShowPrintPreview Creates the print document and displays the Print Preview of the document.
ChartControl.ShowRibbonPrintPreview Creates the print document and displays the Print Preview with the Ribbon toolbar of the document.

To export the chart, use the appropriate ExportTo~ method (e.g., ChartControl.ExportToHtml, ChartControl.ExportToPdf, etc.)

Important

Note that, exporting to raster and vector images is implemented by the Chart and does not require any library.

The chart can be previewed, printed and exported to other formats only if the Printing library is available. Make sure you add a reference to the DevExpress.XtraPrinting.v21.1 assembly.

Also note that chart export to PDF requires the DevExpress.Pdf.Core library to be available.

Example

The following example exports a chart to a PDF format. In this example, the form contains the Export to File and Export to Stream buttons that allow you to save the PDF document with the chart to a file or a stream.

Follow the steps below to implement this scenario:

View Example: How to: Export a Chart to PDF

using DevExpress.XtraCharts;
using DevExpress.XtraPrinting;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace ExportToPDF {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            Series series = new Series("Series1", ViewType.Bar);
            chartControl1.Series.Add(series);
            chartControl1.DataSource = GetSales();
            series.ArgumentDataMember = "Region";
            series.ValueDataMembers.AddRange(new string[] { "Sales" });
        }

        private DataTable GetSales() {
            int prevYear = DateTime.Now.Year - 1;
            DataTable table = new DataTable();
            table.Columns.Add("Region", typeof(string));
            table.Columns.Add("Sales", typeof(decimal));

            table.Rows.Add("Asia", 4.2372D);
            table.Rows.Add("Australia", 1.7871D);
            table.Rows.Add("Europe", 3.0884D);
            table.Rows.Add("North America", 3.4855D);
            table.Rows.Add("South America", 1.6027D);

            return table;
        }

        private void simpleButton1_Click(object sender, EventArgs e) {
            if (chartControl1.IsPrintingAvailable) {
                // The PDF file name.
                string fileName = "Output.pdf";

                // Path to the PDF file.
                string filePath = "c:\\temp";
                if (!Directory.Exists(filePath))
                    Directory.CreateDirectory(filePath);

                string fullPath = String.Format("{0}\\{1}", filePath, fileName);

                // Exports the chart in the vector-based format to a PDF file.
                chartControl1.ExportToPdf(fullPath, new PdfExportOptions { ConvertImagesToJpeg = false });
            }
        }
        private void simpleButton2_Click(object sender, EventArgs e) {
            if (chartControl1.IsPrintingAvailable) {
                // The PDF file name.
                string fileName = "Output.pdf";

                // Path to the PDF file.
                string filePath = "c:\\temp";
                if (!Directory.Exists(filePath))
                    Directory.CreateDirectory(filePath);

                string fullPath = String.Format("{0}\\{1}", filePath, fileName);

                // Exports to a stream as PDF.
                FileStream pdfStream = new FileStream(fullPath, FileMode.Create);
                chartControl1.ExportToPdf(pdfStream);
                pdfStream.Close();
            }
        }
    }
}
See Also