How to: Export a Workbook to PDF
- 4 minutes to read
Use the IWorkbook.ExportToPdf method to save a workbook as a PDF file.
The following code sample generates a PDF file from a workbook and saves it to a stream.
using DevExpress.Spreadsheet;
using System.IO;
//...
// Access a workbook.
IWorkbook workbook = spreadsheetControl.Document;
//...
// Save the workbook as a PDF file.
using (FileStream pdfFileStream = new FileStream("Documents\\Document_PDF.pdf", FileMode.Create)) {
workbook.ExportToPdf(pdfFileStream);
}
Note
Refer to the the following topic for more information on PDF export features and limitations: Export to PDF.
Export a Workbook to Accessible PDF
You can save a workbook as a tagged (accessible) PDF document. Accessible PDF formats allow users with disabilities to use screen readers and other assistive technologies to read information from PDF documents.
You can generate PDF files that conform to the following standards:
- PDF/UA
- PDF/A-1a
- PDF/A-2a
- PDF/A-3a
Generate PDF/UA Documents
The PDF/UA (Universal Accessibility) standard contains requirements for PDF documents to ensure accessibility and support for assistive technology. Requirements for PDF/UA compliance are consistent with the Web Content Accessibility Guidelines (WCAG) 2.0.
Below are some of the constraints the standard defines for PDF documents:
All meaningful content should be tagged and included in the structure tree of document tags. Objects that are not relevant for understanding document content are marked as artifacts (for example, background and decorative images, repeating information in headers and footers, page numbers).
The structure tree generated by document tags must reflect the document’s logical reading order.
Information should not be conveyed by visual means alone (for example, contrast, color, or position on the page).
All pictorial elements (image objects and other non-text objects such as vector objects or object groups) should have alternative text.
All fonts should be embedded.
To generate a document that conforms to the PDF/UA standard, create a PdfExportOptions class instance and set the PdfExportOptions.PdfUACompatibility property to PdfUA1. Pass the class instance to the IWorkbook.ExportToPdf method to save a workbook as a PDF/UA document.
using DevExpress.Spreadsheet;
using DevExpress.XtraPrinting;
// ...
// Access a workbook.
IWorkbook workbook = spreadsheetControl.Document;
// Load a document.
workbook.LoadDocument(@"Documents\Document.xlsx", DocumentFormat.Xlsx);
// Specify PDF export options.
PdfExportOptions options = new PdfExportOptions();
// Specify document compatibility with the PDF/UA specification.
options.PdfUACompatibility = PdfUACompatibility.PdfUA1;
// Export the workbook to PDF.
workbook.ExportToPdf(@"Documents\Output_Workbook.pdf", options);
Generate Accessible PDF/A Documents
PDF/A is an archival PDF format used for long-term preservation of electronic documents. The PDF/A standard includes the following restrictions:
- All PDF/A versions implicitly prohibit encryption.
- All fonts that are used in PDF/A documents should be embedded.
- The PDF/A-1 and PDF/A-2 standards do not support attachments.
- The PDF/A-1 standard does not support transparency (the alpha channel in images is ignored).
To export a workbook to a PDF document that supports the PDF/A Conformance Level A (Accessible), create a PdfExportOptions class instance and set the PdfExportOptions.PdfACompatibility property to one of the following values: PdfA1a, PdfA2a, or PdfA3a. Pass the class instance to the IWorkbook.ExportToPdf method to generate a PDF file.
The following code sample saves a workbook as a PDF file that conforms to the PDF/A-3a standard:
using DevExpress.Spreadsheet;
using DevExpress.XtraPrinting;
// ...
// Access a workbook.
IWorkbook workbook = spreadsheetControl.Document;
// Load a document.
workbook.LoadDocument(@"Documents\Document.xlsx", DocumentFormat.Xlsx);
// Specify PDF export options.
PdfExportOptions options = new PdfExportOptions();
// Specify document compatibility with the PDF/A-3a specification.
options.PdfACompatibility = PdfACompatibility.PdfA3a;
// Export the workbook to PDF.
workbook.ExportToPdf(@"Documents\Output_Workbook.pdf", options);
Generate Accessible PDF Documents in the User Interface
The WPF Spreadsheet control’s UI allows you to save a workbook as an accessible PDF document. Invoke the Print Preview Dialog, click the Export button, and select PDF File.
In the invoked Export Document dialog, expand the PDF/A compatibility or PDF/UA compatibility drop-down list and select an accessible PDF format you want to use.