Back to Devexpress

XtraReport.ExportToPdf(String, PdfExportOptions) Method

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-xtrareport-dot-exporttopdf-x28-system-dot-string-devexpress-dot-xtraprinting-dot-pdfexportoptions-x29.md

latest11.5 KB
Original Source

XtraReport.ExportToPdf(String, PdfExportOptions) Method

SECURITY-RELATED CONSIDERATIONS

Using file paths sourced from untrusted input may expose unauthorized files or allow unintended file access. Always validate and normalize all external paths to prevent path manipulation.

Exports a report to the specified file in PDF format.

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public void ExportToPdf(
    string path,
    PdfExportOptions options = null
)
vb
Public Sub ExportToPdf(
    path As String,
    options As PdfExportOptions = Nothing
)

Parameters

NameTypeDescription
pathString

The path to the exported PDF file.

|

Optional Parameters

NameTypeDefaultDescription
optionsPdfExportOptionsnull

The PDF export options. You can omit this parameter to use the current report export options.

|

Remarks

Note

Once the document export has started, it runs to completion and you cannot interrupt or cancel it.

This method exports a report to a file in PDF format with the specified PDF export options.

If you do not specify export options, the method uses the current report export options. To access the report export options, use the XtraReport.ExportOptions.Pdf notation.

Important

This method overwrites files with the same name without confirmation.

Use the ExportToPdfAsync(String, PdfExportOptions, CancellationToken) method instead of ExportToPdf to export a report asynchronously in a separate task.

Example

This example demonstrates how to export a report to PDF format.

The project uses the XtraReport.ExportToPdf method with the PdfExportOptions object as a parameter.

Note

The complete sample project How to export a report to PDF format is available in the DevExpress Examples repository.

vb
Imports DevExpress.XtraPrinting
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
' ...

Namespace WindowsFormsApplication1
    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
            ' A path to export a report.
            Dim reportPath As String = "c:\\Temp\Test.pdf"

            Using report As New XtraReport1()
                ' Specify PDF-specific export options.
                Dim pdfOptions As PdfExportOptions = report.ExportOptions.Pdf

                ' Specify the pages to be exported.
                pdfOptions.PageRange = "1, 3-5"

                ' Specify the quality of exported images.
                pdfOptions.ConvertImagesToJpeg = False
                pdfOptions.ImageQuality = PdfJpegImageQuality.Medium

                ' Specify the PDF/A-compatibility.
                pdfOptions.PdfACompatibility = PdfACompatibility.PdfA3b

                ' The following options are not compatible with PDF/A.
                ' The use of these options will result in errors on PDF validation.
                'pdfOptions.NeverEmbeddedFonts = "Tahoma;Courier New";
                'pdfOptions.ShowPrintDialogOnOpen = true;

                ' If required, you can specify the security and signature options. 
                'pdfOptions.PasswordSecurityOptions
                'pdfOptions.SignatureOptions

                ' If required, specify necessary metadata and attachments
                ' (e.g., to produce a ZUGFeRD-compatible PDF).
                'pdfOptions.AdditionalMetadata
                'pdfOptions.Attachments

                ' Specify the document options.
                pdfOptions.DocumentOptions.Application = "Test Application"
                pdfOptions.DocumentOptions.Author = "DX Documentation Team"
                pdfOptions.DocumentOptions.Keywords = "DevExpress, Reporting, PDF"
                pdfOptions.DocumentOptions.Producer = Environment.UserName.ToString()
                pdfOptions.DocumentOptions.Subject = "Document Subject"
                pdfOptions.DocumentOptions.Title = "Document Title"

                ' Checks the validity of PDF export options 
                ' and return a list of any detected inconsistencies.
                Dim result As IList(Of String) = pdfOptions.Validate()
                If result.Count > 0 Then
                    Console.WriteLine(String.Join(Environment.NewLine, result))
                Else
                    report.ExportToPdf(reportPath, pdfOptions)
                End If
            End Using

        End Sub

    End Class
End Namespace
csharp
using DevExpress.XtraPrinting;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
// ...

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

        private void button1_Click(object sender, EventArgs e) {
            // A path to export a report.
            string reportPath = @"c:\\Temp\Test.pdf";

            using (XtraReport1 report = new XtraReport1()) {
                // Specify PDF-specific export options.
                PdfExportOptions pdfOptions = report.ExportOptions.Pdf;

                // Specify the pages to be exported.
                pdfOptions.PageRange = "1, 3-5";

                // Specify the quality of exported images.
                pdfOptions.ConvertImagesToJpeg = false;
                pdfOptions.ImageQuality = PdfJpegImageQuality.Medium;

                // Specify the PDF/A-compatibility.
                pdfOptions.PdfACompatibility = PdfACompatibility.PdfA3b;

                // The following options are not compatible with PDF/A.
                // The use of these options will result in errors on PDF validation.
                //pdfOptions.NeverEmbeddedFonts = "Tahoma;Courier New";
                //pdfOptions.ShowPrintDialogOnOpen = true;

                // If required, you can specify the security and signature options. 
                //pdfOptions.PasswordSecurityOptions
                //pdfOptions.SignatureOptions

                // If required, specify necessary metadata and attachments
                // (e.g., to produce a ZUGFeRD-compatible PDF).
                //pdfOptions.AdditionalMetadata
                //pdfOptions.Attachments

                // Specify the document options.
                pdfOptions.DocumentOptions.Application = "Test Application";
                pdfOptions.DocumentOptions.Author = "DX Documentation Team";
                pdfOptions.DocumentOptions.Keywords = "DevExpress, Reporting, PDF";
                pdfOptions.DocumentOptions.Producer = Environment.UserName.ToString();
                pdfOptions.DocumentOptions.Subject = "Document Subject";
                pdfOptions.DocumentOptions.Title = "Document Title";

                // Checks the validity of PDF export options 
                // and return a list of any detected inconsistencies.
                IList<string> result = pdfOptions.Validate();
                if (result.Count > 0)
                    Console.WriteLine(String.Join(Environment.NewLine, result));
                else
                    report.ExportToPdf(reportPath, pdfOptions);
            }

        }

    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the ExportToPdf(String, PdfExportOptions) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

xaf-how-to-create-and-setup-an-xtrareport-report-for-exporting-to-a-stream-in-a-non-xaf/CS/XPO/ExportReport/ExportXAFReport/Program.cs#L28

csharp
reportExportService.SetupReport(report);
report.ExportToPdf("testXPO.pdf");

reporting-winforms-export-pdf-zugferd/CS/ZUGFeRD_sample/Form1.cs#L38

csharp
report.ExportToPdf("germanResult.pdf", options);
Process.Start("germanResult.pdf");

reporting-winforms-export-pdf-digital-signature/CS/pdf-signature-sample/Form1.cs#L48

csharp
// Export the report to PDF.
report.ExportToPdf("test.pdf");

maui-generate-data-bound-report/CS/MauiReportingApp/MainPage.xaml.cs#L27

csharp
string resultFile = Path.Combine(FileSystem.Current.AppDataDirectory, report.Name + ".pdf");
report.ExportToPdf(resultFile);
pdfViewer.DocumentSource = PdfDocumentSource.FromFile(resultFile);

reporting-winforms-export-pdf-zugferd/VB/ZUGFeRD_sample/Form1.vb#L34

vb
report.ExportToPdf("germanResult.pdf", options)
End Using

reporting-winforms-export-pdf-digital-signature/VB/pdf-signature-sample/Form1.vb#L45

vb
' created in the same folder where the application's .exe file is located.
report.ExportToPdf("test.pdf")
System.Diagnostics.Process.Start("test.pdf")

See Also

Export Reports

Export to PDF

XtraReport Class

XtraReport Members

DevExpress.XtraReports.UI Namespace