下面的示例演示了如何把报表导出为 PDF 格式。 要完成此任务,需使用 XtraReport.ExportToPdf 方法。 此示例也演示了当报表被导出为 PDF 时,哪些特定的 PdfExportOptions 可能被使用。

为了使此示例工作,首先需要 添加新报表 (命名为 XtraReport1) 到项目中,拖放一些报表控件到报表中,然后执行下面的代码 (例如,在窗体上任一按钮的 Button.Click 事件处理程序中)。 也要注意,如果想让结果文件自动被系统中默认的用于打开 *.pdf 文件的程序打开,那么可以调用在本例中也展示了的 StartProcess 方法。

CodeCentralShow Me

在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E44。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。

C#CopyCode image复制代码
using System.Diagnostics;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...

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

    // Create a report instance.
    XtraReport1 report = new XtraReport1();

    // Get its PDF export options.
    PdfExportOptions pdfOptions = report.ExportOptions.Pdf;

    // Set PDF-specific export options.
    pdfOptions.Compressed = true;
    pdfOptions.ImageQuality = PdfJpegImageQuality.Low;
    pdfOptions.NeverEmbeddedFonts = "Tahoma;Courier New";
    pdfOptions.DocumentOptions.Application = "Test Application";
    pdfOptions.DocumentOptions.Author = "DX Documentation Team";
    pdfOptions.DocumentOptions.Keywords = "XtraReports, XtraPrinting";
    pdfOptions.DocumentOptions.Subject = "Test Subject";
    pdfOptions.DocumentOptions.Title = "Test Title";

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

    // Export the report to PDF.
    report.ExportToPdf(reportPath);

    // Show the result.
    StartProcess(reportPath);
}

// Use this method if you want to automaically open
// the created PDF file in the default program.
public void StartProcess(string path)
{
    Process process = new Process();
    try
    {
        process.StartInfo.FileName = path;
        process.Start();
        process.WaitForInputIdle();
    }
    catch { }
}
Visual BasicCopyCode image复制代码
Imports System.Diagnostics
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
' ...

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

    ' Create a report instance.
    Dim report As New XtraReport1()

    ' Get its PDF export options.
    Dim pdfOptions As PdfExportOptions = report.ExportOptions.Pdf

    ' Set PDF-specific export options.
    pdfOptions.Compressed = True
    pdfOptions.ImageQuality = PdfJpegImageQuality.Low
    pdfOptions.NeverEmbeddedFonts = "Tahoma;Courier New"
    pdfOptions.DocumentOptions.Application = "Test Application"
    pdfOptions.DocumentOptions.Author = "DX Documentation Team"
    pdfOptions.DocumentOptions.Keywords = "XtraReports, XtraPrinting"
    pdfOptions.DocumentOptions.Subject = "Test Subject"
    pdfOptions.DocumentOptions.Title = "Test Title"

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

    ' Export the report to PDF.
    report.ExportToPdf(reportPath)

    ' Show the result.
    StartProcess(reportPath)
End Sub

' Use this method if you want to automaically open
' the created PDF file in the default program.
Public Sub StartProcess(ByVal path As String)
    Dim process As New Process()
    Try
        process.StartInfo.FileName = path
        process.Start()
        process.WaitForInputIdle()
    Catch
    End Try
End Sub

Expand image参阅