Export Workbooks to PDF
- 8 minutes to read
Use the Workbook.ExportToPdf method to export a workbook to PDF. You can export the entire workbook or specify worksheets to export. Use PdfExportOptions to configure export settings.
Note
The Spreadsheet Document API implements Excel behavior when it exports a workbook with empty worksheets. The API does not export empty worksheets to the PDF.
If the workbook contains only empty worksheets, the resulting PDF has no pages. When you open the PDF document in a PDF viewer (for example, Adobe Acrobat Reader), the viewer displays the following message: “There was an error opening this document. This file cannot be opened because it has no pages.”
Export a Workbook to a PDF File
Call the Workbook.ExportToPdf(outputStream) method to export a workbook to the specified file stream.
The following code snippet loads an existing Excel workbook and exports it to a PDF file:
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
using FileStream inputStream = File.OpenRead("Document.xlsx");
using Workbook workbook = new Workbook(inputStream);
using FileStream outputStream = File.Create(Path.Combine("output", "Document.pdf"));
workbook.ExportToPdf(outputStream);
Export a Workbook to a Byte Array
Call the Workbook.ExportToPdf() method to obtain the exported PDF as a byte array.
The following code snippet loads a workbook and exports it to a byte array:
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
using FileStream inputStream = File.OpenRead("Document.xlsx");
using Workbook workbook = new Workbook(inputStream);
byte[] pdf = workbook.ExportToPdf();
Configure PDF Export Options
Use the PdfExportOptions class to configure PDF export settings.
Property | Description |
|---|---|
Specifies whether to export hidden worksheets. | |
Specifies whether to calculate formulas before export. | |
Specifies how the API exports document metadata to the PDF. Available modes include:
| |
Specifies whether to rasterize images. | |
Specifies the resolution for image rasterization. | |
Specifies whether to convert images to JPEG. | |
Specifies image quality. | |
Specifies whether to display the print dialog when the exported PDF opens. | |
Specifies fonts that should not be embedded in the PDF. | |
Contains PDF signature options. | |
Contains PDF document properties. | |
Contains PDF encryption options. | |
Specifies PDF/A compatibility. | |
Specifies PDF/UA compatibility. | |
Accesses a collection of PDF file attachments. |
Pass the configured PdfExportOptions object to the corresponding Workbook.ExportToPdf method to configure the exported PDF document:
using System.IO;
using DevExpress.Docs.Spreadsheet;
using DevExpress.Docs.Spreadsheet.Export;
Directory.CreateDirectory("output");
using FileStream inputStream = File.OpenRead("Document.xlsx");
using FileStream outputStream = File.Create(Path.Combine("output", "Document.pdf"));
using Workbook workbook = new Workbook(inputStream);
PdfExportOptions exportOptions = new PdfExportOptions()
{
ExportHiddenSheets = true,
ConvertImagesToJpeg = true,
ImageQuality = DevExpress.Docs.Pdf.ImageQuality.Low
};
workbook.ExportToPdf(outputStream, exportOptions);
Export a Workbook to Accessible PDF
You can export a workbook to a PDF document that conforms to PDF/UA or PDF/A standards.
Use the PdfExportOptions class to specify the required compatibility level. PdfUACompatibility and PdfACompatibility enumerations are in the DevExpress.Docs.Pdf namespace.
Export to PDF/UA
PDF/UA (Universal Accessibility) is an accessibility standard for PDF documents. It defines requirements that help users with disabilities access document content with screen readers and other assistive technologies.
Supported PDF/UA compatibility levels:
- PDF/UA-1
- PDF/UA-2
PDF/UA standard requirements include:
- Tag all meaningful content in the document tag structure. Mark non-content objects, such as background and decorative images, headers, footers, and page numbers, as artifacts. Use the ShapeBase.Decorative property to mark images as decorative.
- Preserve the document’s logical reading order in the tag structure.
- Do not convey information through visual properties alone, such as color, contrast, or position.
- Specify alternative text for images, vector objects, and object groups.
- Embed all fonts.
Set the PdfUACompatibility property to PdfUA1 to export a workbook to a PDF/UA document.
You can combine the PDF/UA-1 level with a PDF/A level. PDF/UA-2 is not compatible with PDF/A levels.
The following code snippet exports a workbook to a PDF/UA document:
using System.IO;
using DevExpress.Docs.Pdf;
using DevExpress.Docs.Spreadsheet;
using DevExpress.Docs.Spreadsheet.Export;
Directory.CreateDirectory("output");
using FileStream inputStream = File.OpenRead("Document.xlsx");
using Workbook workbook = new Workbook(inputStream);
PdfExportOptions exportOptions = new PdfExportOptions {
PdfUACompatibility = PdfUACompatibility.PdfUA1
};
using FileStream outputStream = File.Create(Path.Combine("output", "Document_UACompatible.pdf"));
workbook.ExportToPdf(outputStream, exportOptions);
Export to PDF/A
PDF/A is an archival standard for the long-term storage of electronic documents. It defines requirements that help ensure that PDF documents remain accessible and reproducible over time.
Supported PDF/A compatibility levels:
- PDF/A-1a
- PDF/A-1b
- PDF/A-2a
- PDF/A-2b
- PDF/A-3a
- PDF/A-3b
PDF/A standard requirements include:
- PDF/A documents cannot use encryption.
- Embed all fonts used in PDF/A documents.
- PDF/A-1 and PDF/A-2 do not support attachments.
- PDF/A-1 does not support transparency. The alpha channel in images is ignored.
Set the PdfACompatibility property to the required PDF/A compatibility level to export a workbook to a PDF/A document.
If other export options conflict with the specified PDF/A or PDF/UA level (for example, a PDF/A-1 document with attachments), the Workbook.ExportToPdf method throws a PdfExportException. The PdfExportException.ValidationErrors property gets all validation errors.
The following code snippet exports a workbook to a PDF/A-3a document:
using System.IO;
using DevExpress.Docs.Pdf;
using DevExpress.Docs.Spreadsheet;
using DevExpress.Docs.Spreadsheet.Export;
Directory.CreateDirectory("output");
using FileStream inputStream = File.OpenRead("Document.xlsx");
using Workbook workbook = new Workbook(inputStream);
PdfExportOptions exportOptions = new PdfExportOptions {
PdfACompatibility = PdfACompatibility.PdfA3a
};
using FileStream outputStream = File.Create(Path.Combine("output", "Document_A3aCompatible.pdf"));
workbook.ExportToPdf(outputStream, exportOptions);
Export Specific Worksheets
Pass worksheet indexes to the Workbook.ExportToPdf(Stream, Int32[]) method to export specific worksheets.
The following code snippet exports the first two worksheets in a workbook to a PDF file:
using System;
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
using FileStream inputStream = File.OpenRead("Document.xlsx");
using Workbook workbook = new Workbook(inputStream);
using FileStream outputStream = File.Create(Path.Combine("output", "Document.pdf"));
try {
workbook.ExportToPdf(outputStream, new int[] { 0, 1 });
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
Warning
The Spreadsheet Document API throws a “Cannot export the document to PDF” exception if the workbook does not contain a worksheet at the specified index.
Export a Workbook without Formula Calculation
The Spreadsheet Document API calculates formulas that have outdated results before it exports a workbook to PDF. Set the PdfExportOptions.CalculateBeforeExport property to false to skip calculation and export formula results stored in the document.
Important
Disable calculation only for workbooks that you do not modify. If CalculateBeforeExport is false, the PDF can contain outdated formula results. If you assign a formula in code and do not calculate the formula, the PDF displays an empty cell instead of the formula result.
The following code snippet exports a workbook to PDF without formula calculation:
using System.IO;
using DevExpress.Docs.Spreadsheet;
using DevExpress.Docs.Spreadsheet.Export;
Directory.CreateDirectory("output");
using FileStream inputStream = File.OpenRead("Document.xlsx");
using Workbook workbook = new Workbook(inputStream);
PdfExportOptions exportOptions = new PdfExportOptions {
CalculateBeforeExport = false
};
using FileStream outputStream = File.Create(Path.Combine("output", "Document.pdf"));
workbook.ExportToPdf(outputStream, exportOptions);