Back to Devexpress

How to: Export a Chart to PDF

windowsforms-2506-controls-and-libraries-chart-control-examples-producing-output-how-to-export-a-chart-to-pdf.md

latest9.3 KB
Original Source

How to: Export a Chart to PDF

  • Dec 25, 2020
  • 4 minutes to read

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

csharp
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();
            }
        }
    }
}
vb
Imports DevExpress.XtraCharts
Imports DevExpress.XtraPrinting
Imports System
Imports System.Data
Imports System.IO
Imports System.Windows.Forms

Namespace ExportToPDF
    Public Partial Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim series As Series = New Series("Series1", ViewType.Bar)
            chartControl1.Series.Add(series)
            chartControl1.DataSource = GetSales()
            series.ArgumentDataMember = "Region"
            series.ValueDataMembers.AddRange(New String() {"Sales"})
        End Sub

        Private Function GetSales() As DataTable
            Dim prevYear As Integer = Date.Now.Year - 1
            Dim table As DataTable = New DataTable()
            table.Columns.Add("Region", GetType(String))
            table.Columns.Add("Sales", GetType(Decimal))
            table.Rows.Add("Asia", 4.2372R)
            table.Rows.Add("Australia", 1.7871R)
            table.Rows.Add("Europe", 3.0884R)
            table.Rows.Add("North America", 3.4855R)
            table.Rows.Add("South America", 1.6027R)
            Return table
        End Function

        Private Sub simpleButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
            If chartControl1.IsPrintingAvailable Then
                ' The PDF file name.
                Dim fileName As String = "Output.pdf"

                ' Path to the PDF file.
                Dim filePath As String = "c:\temp"
                If Not Directory.Exists(filePath) Then Directory.CreateDirectory(filePath)
                Dim fullPath As String = String.Format("{0}\{1}", filePath, fileName)

                ' Exports the chart in the vector-based format to the PDF file.
                chartControl1.ExportToPdf(fullPath, New PdfExportOptions With {
                                                    .ConvertImagesToJpeg = False})
            End If
        End Sub

        Private Sub simpleButton2_Click(ByVal sender As Object, ByVal e As EventArgs)
            If chartControl1.IsPrintingAvailable Then
                ' The PDF file name.
                Dim fileName As String = "Output.pdf"

                ' Path to the PDF file.
                Dim filePath As String = "c:\temp"
                If Not Directory.Exists(filePath) Then Directory.CreateDirectory(filePath)
                Dim fullPath As String = String.Format("{0}\{1}", filePath, fileName)

                ' Exports to a stream as PDF.
                Dim pdfStream As FileStream = New FileStream(fullPath, FileMode.Create)
                chartControl1.ExportToPdf(pdfStream)
                pdfStream.Close()
            End If
        End Sub
    End Class
End Namespace

See Also

Chart Control. Print and Export.