Skip to main content

Export a Control in Various Formats (PDF, HTML, BMP, etc.) Using the XtraPrinting Library

  • 2 minutes to read

The Printing Library allows you to export DevExpress .NET controls to various document formats (PDF, HTML, MHT, TXT, CSV, XLS, RTF) and image formats (BMP, JPEG, GIF, TIFF, PNG, EMF). This topic demonstrates how to export a control to PDF format with the Print Preview window and via code.

Note

Note that the XtraPrinting Library cannot export certain controls to specific formats. All controls, however, can be exported to PDF or image format.

Export Using the Print Preview Window

You can use the Print Preview window to export a printable control to an available format.

Use the control’s ShowPrintPreview method to display this window. The following image illustrates a Print Preview window for a Grid Control.

CD_ExportViaPrinting_PrintPreview

A toolbar at the top of the Print Preview form displays the Export Document… button. Click this button to show a drop-down menu that allows you to choose the required export file format.

CD_ExportViaPrinting_ExportDocumentButton

To add page or report headers to the report, generate a report using printable link (PrintableComponentLink). This approach is described in the How to: Set Paper Format and Add Custom Information to the Report when Printing/Exporting a Control topic.

Export in Code

The following code demonstrates how to export a control to a PDF file in code using XtraPrinting Library methods without the Print Preview window. The control (XtraGrid) is exported to PDF via the PrintingSystemBase.ExportToPdf method. For more information on how to print and export controls, refer to the Printing Library documentation.

using DevExpress.XtraPrinting;

// Create a PrintingSystem component.
DevExpress.XtraPrinting.PrintingSystem ps = new DevExpress.XtraPrinting.PrintingSystem();

// Create a link that will print a control.
DevExpress.XtraPrinting.PrintableComponentLink link = new PrintableComponentLink(ps);

// Specify the control to be printed.
link.Component = gridControl1;

// Generate a report.
link.CreateDocument();

// Export the report to a PDF file.
string filePath = @"c:\gridcontrol.pdf";
link.PrintingSystem.ExportToPdf(filePath);

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = filePath;
process.Start();
See Also