Previous topicNext topic
Help > 开发指南 > Excel > API > 示例 > Workbook >
如何:将工作簿导出为 PDF

导出工作簿

使用 Workbook.ExportToPdf 方法重载将工作簿导出为 PDF。以下代码示例将文档以 PDF 格式保存到流中:

Vb.Net
'创建一个Workbook工作簿对象.
Using workbook As New DevExpress.Spreadsheet.Workbook()
    '从文件中加载Workbook工作簿
    workbook.LoadDocument("D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx)
    ' ...
    '导出workbook工作簿到PDF.
    Using pdfFileStream As New FileStream("D:\Document_PDF.pdf", FileMode.Create)
        workbook.ExportToPdf(pdfFileStream)
    End Using
End Using

C#
// 创建一个Workbook工作簿对象.
using (DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook())
{
    // 从文件中加载Workbook工作簿
    workbook.LoadDocument(@"D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx);
    // ...
    // 导出workbook工作簿到PDF.
    using (FileStream pdfFileStream = new FileStream(@"D:\Document_PDF.pdf", FileMode.Create))
    {
        workbook.ExportToPdf(pdfFileStream);
    }
}


将工作簿导出为具有辅助工具的 PDF

您可以将工作簿另存为带标签(可访问)的 PDF 文档。辅助 PDF 格式允许残障用户使用屏幕阅读器和其他辅助技术来阅读 PDF 文档中的信息。

您可以生成符合以下标准的 PDF 文件:

生成 PDF/UA 文档

PDF/UA(通用辅助功能)标准包含对 PDF 文档的要求,以确保辅助技术的可访问性和支持。PDF/UA 合规性要求与 Web 内容可访问性指南 (WCAG) 2.0 一致。

以下是该标准为 PDF 文档定义的一些限制:

若要生成符合 PDF/UA 标准的文档,请创建一个 PdfExportOptions 类实例,并将 PdfExportOptions.PdfUACompatibility 属性设置为 PdfUA1。将类实例传递给 Workbook.ExportToPdf 方法,以将工作簿另存为 PDF/UA 文档。

Vb.Net
'创建一个Workbook工作簿对象.
Using workbook As New DevExpress.Spreadsheet.Workbook()
    '从文件中加载Workbook工作簿
    workbook.LoadDocument("D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx)
    ' ...
    '指定PDF导出选项.
    Dim options As New DevExpress.XtraPrinting.PdfExportOptions()
    ' 指定文档与PDF/UA规范的兼容性。
    options.PdfUACompatibility = DevExpress.XtraPrinting.PdfUACompatibility.PdfUA1

    '导出workbook工作簿到PDF文件.
    workbook.ExportToPdf("D:\Output_Workbook.pdf", options)
End Using

C#
// 创建一个Workbook工作簿对象.
using (DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook())
{
    // 从文件中加载Workbook工作簿
    workbook.LoadDocument(@"D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx);
    // ...
    // 指定PDF导出选项.
    DevExpress.XtraPrinting.PdfExportOptions options = new DevExpress.XtraPrinting.PdfExportOptions();
    // 指定文档与PDF/UA规范的兼容性。
    options.PdfUACompatibility = DevExpress.XtraPrinting.PdfUACompatibility.PdfUA1;

    // 导出workbook工作簿到PDF文件.
    workbook.ExportToPdf(@"D:\Output_Workbook.pdf", options);
}

生成可访问的 PDF/A 文档

PDF/A 是一种用于长期保存电子文档的存档 PDF 格式。PDF/A 标准包括以下限制:

若要将工作簿导出到支持 PDF/A 一致性级别 A(可访问)的 PDF 文档,请创建一个 PdfExportOptions 类实例,并将 PdfExportOptions.PdfACompatibility 属性设置为以下值之一:PdfA1a、PdfA2a 或 PdfA3a。 将类实例传递给 Workbook.ExportToPdf 方法以生成 PDF 文件。

以下代码示例将工作簿另存为符合 PDF/A-3a 标准的 PDF 文件:

Vb.Net
'创建一个Workbook工作簿对象.
Using workbook As New DevExpress.Spreadsheet.Workbook()
    '从文件中加载Workbook工作簿
    workbook.LoadDocument("D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx)
    ' ...
    '指定PDF导出选项.
    Dim options As New DevExpress.XtraPrinting.PdfExportOptions()
    '指定文档与PDF/A-3a规范的兼容性。
    options.PdfACompatibility = DevExpress.XtraPrinting.PdfACompatibility.PdfA3a

    '导出workbook工作簿到PDF文件.
    workbook.ExportToPdf("D:\Output_Workbook.pdf", options)
End Using

C#
// 创建一个Workbook工作簿对象.
using (DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook())
{
    // 从文件中加载Workbook工作簿
    workbook.LoadDocument(@"D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx);
    // ...
    // 指定PDF导出选项.
    DevExpress.XtraPrinting.PdfExportOptions options = new DevExpress.XtraPrinting.PdfExportOptions();
    // 指定文档与PDF/A-3a规范的兼容性。
    options.PdfACompatibility = DevExpress.XtraPrinting.PdfACompatibility.PdfA3a;

    // 导出workbook工作簿到PDF文件.
    workbook.ExportToPdf(@"D:\Output_Workbook.pdf", options);
}

导出单个工作表

Workbook.ExportToPdf 方法重载允许您以 PDF 格式保存单个工作表。

下面的代码示例定义了 PDF 导出选项,并从指定的工作表生成 PDF 文件。

Vb.Net
'创建一个Workbook工作簿对象.
Using workbook As New DevExpress.Spreadsheet.Workbook()
    '从文件中加载Workbook工作簿
    workbook.LoadDocument("D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx)
    ' ...
    '指定PDF导出选项.
    Dim options As New DevExpress.XtraPrinting.PdfExportOptions()
    options.DocumentOptions.Author = "John Smith"
    options.ImageQuality = DevExpress.XtraPrinting.PdfJpegImageQuality.Medium

    '导出指定工作表到PDF文件中
    Using pdfFileStream As New FileStream("D:\Document_PDF.pdf", FileMode.Create)
        workbook.ExportToPdf(pdfFileStream, options, "Sheet1", "Sheet2")
    End Using
End Using

C#
// 创建一个Workbook工作簿对象.
using (DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook())
{
    // 从文件中加载Workbook工作簿
    workbook.LoadDocument(@"D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx);
    // ...
    // 指定PDF导出选项.
    DevExpress.XtraPrinting.PdfExportOptions options = new DevExpress.XtraPrinting.PdfExportOptions();
    options.DocumentOptions.Author = "John Smith";
    options.ImageQuality = DevExpress.XtraPrinting.PdfJpegImageQuality.Medium;

    // 导出指定工作表到PDF文件中
    using (FileStream pdfFileStream = new FileStream(@"D:\Document_PDF.pdf", FileMode.Create))
    {
        workbook.ExportToPdf(pdfFileStream, options, "Sheet1", "Sheet2");
    }
}

提示

处理工作簿。在打印工作表事件之前取消特定工作表的导出。

异步导出

使用 Workbook.ExportToPdfAsync 方法将工作簿或单个工作表异步导出为 PDF。方法重载允许您定义导出选项、实现进度通知或根据需要取消操作。

重要

调用此方法时,请考虑以下事项:

以下代码示例将 XLSX 文件异步转换为 PDF,并在操作时间超过 10 秒时取消该操作:

Vb.Net
Private Async Sub ConvertXlsx2PdfWithCancellation()
    ' 指定取消令牌。
    Using source As New CancellationTokenSource(10000)
        '创建一个Workbook工作簿对象.
        Using workbook As New DevExpress.Spreadsheet.Workbook()
            Try
                '异步加载XLSX文件.
                Await workbook.LoadDocumentAsync("D:\Document.xlsx", source.Token)
                '异步将XLSX文件导出到PDF文件.
                Await workbook.ExportToPdfAsync("D:\Result.pdf", source.Token)
            Catch e1 As OperationCanceledException
                MessageBox.Show("Cancelled by timeout.")
            End Try
        End Using
    End Using
End Sub

C#
private async void ConvertXlsx2PdfWithCancellation()
{
    // 指定取消令牌。
    using (CancellationTokenSource source = new CancellationTokenSource(10000))
    {
        // 创建一个Workbook工作簿对象.
        using (DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook())
        {
            try
            {
                // 异步加载XLSX文件.
                await workbook.LoadDocumentAsync(@"D:\Document.xlsx", source.Token);
                // 异步将XLSX文件导出到PDF文件.
                await workbook.ExportToPdfAsync(@"D:\Result.pdf", source.Token);
            }
            catch (OperationCanceledException e1)
            {
                MessageBox.Show("Cancelled by timeout.");
            }
        }
    }
}


导出前计算公式

工作簿的默认计算模式为“手动”。此模式意味着电子表格组件在生成 PDF 文档之前不会重新计算公式。在将工作簿导出为 PDF 之前,调用 Workbook.Calculate 或 Workbook.CalculateFull 方法计算工作簿中的所有公式。

Vb.Net
'创建一个Workbook工作簿对象.
Using workbook As New DevExpress.Spreadsheet.Workbook()
    '从文件中加载Workbook工作簿
    workbook.LoadDocument("D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx)
    ' ...
    '在导出PDF文件前,重新计算工作簿中所有的公式
    workbook.Calculate()
    '导出workbook工作簿到PDF文件.
    workbook.ExportToPdf("D:\Output_Workbook.pdf")
End Using

C#
// 创建一个Workbook工作簿对象.
using (DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook())
{
    // 从文件中加载Workbook工作簿
    workbook.LoadDocument(@"D:\Document.xlsx", DevExpress.Spreadsheet.DocumentFormat.Xlsx);
    // ...
    //在导出PDF文件前,重新计算工作簿中所有的公式
    workbook.Calculate();
    // 导出workbook工作簿到PDF文件.
    workbook.ExportToPdf(@"D:\Output_Workbook.pdf");
}