Skip to main content

WebDocumentViewerOperationLogger.ExportDocumentStarting(String, String, String, ExportOptions, PrintingSystemBase, Func<ExportedDocument>) Method

The method allows you to implement custom logic for asynchronous export of a report document.

Namespace: DevExpress.XtraReports.Web.WebDocumentViewer

Assembly: DevExpress.XtraReports.v24.1.Web.dll

NuGet Package: DevExpress.Web.Reporting.Common

Declaration

public virtual ExportedDocument ExportDocumentStarting(
    string documentId,
    string asyncExportOperationId,
    string format,
    ExportOptions options,
    PrintingSystemBase printingSystem,
    Func<ExportedDocument> doExportSynchronously
)

Parameters

Name Type Description
documentId String

A String value that identifies a document.

asyncExportOperationId String

A String value that identifies the export operation.

format String

A String value that specifies the export format.

options ExportOptions

An ExportOptions object.

printingSystem PrintingSystemBase

A PrintingSystemBase object.

doExportSynchronously Func<ExportedDocument>

A System.Func delegate.

Returns

Type Description
ExportedDocument

An exported document.

Remarks

You can override the ExportDocumentStarting method to implement custom export procedures that allows you to modify a report before export or post-process an exported document.

Note

Register the MyOperationLogger service as described in the WebDocumentViewerOperationLogger topic.

Example: Override the Export to PDF/XLS/XLSX Action

The following code creates a “Hello World” report document and export it to PDF when the user executes the Export to PDF command. The command Export to XLS or Export to XLSX instantiates a custom report, modifies it and exports to a spreadsheet file.

using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Web.ClientControls;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using System;
using System.IO;

public class MyOperationLogger : WebDocumentViewerOperationLogger
{
    public override ExportedDocument ExportDocumentStarting(string documentId, 
    tring asyncExportOperationId, string format, ExportOptions options, 
    PrintingSystemBase printingSystem, Func<ExportedDocument> doExportSynchronously)
    {
        switch (format)
        {
            case "pdf":
                {
                    // Creates a one-page NEW report.
                    var report = new XtraReport();
                    var detail = new DetailBand();
                    detail.Controls.Add(new XRLabel() { Text = "Hello World!" });
                    report.Bands.Add(detail);
                    byte[] bytes;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        report.ExportToPdf(ms, options.Pdf);
                        bytes = ms.ToArray();
                    }
                    return new ExportedDocument(bytes, @"application/pdf", "inline", "SampleOnePageReport.pdf");
                }
            case "xls":
            case "xlsx":
                {
                    // Modifies an existing report.
                    var report = new TestReport();
                    XRTable table = report.FindControl("table3", true) as XRTable;
                    table.BackColor = System.Drawing.Color.LightGreen;
                    byte[] bytes;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        report.ExportToXlsx(ms, options.Xlsx);
                        bytes = ms.ToArray();
                    }
                    return new ExportedDocument(bytes, @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "inline", "SampleGreenCells.xlsx");
                }
            default: return base.ExportDocumentStarting(documentId, 
            asyncExportOperationId, format, options, printingSystem, doExportSynchronously);
        }
    }
}

Example: Implement a Custom Export Action

You can implement a custom export action that is performed when the user selects Image: JPEG item in the Export drop-down menu.

The following code is the CustomizeMenuActions event handler that adds a new command item to the Document Viewer menu:

function onCustomizeMenu(s, e) {
        var actionExportTo = e.GetById(DevExpress.Reporting.Viewer.ActionId.ExportTo);
        actionExportTo.items()[0].items.push({
            format: "JPEG",
            text: "Image: JPEG",
        });
}

The following code creates a TestReport document and exports it to JPEG format:

using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Web.ClientControls;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using System;
using System.Drawing.Imaging;
using System.IO;

public class MyOperationLogger : WebDocumentViewerOperationLogger
{
    public override ExportedDocument ExportDocumentStarting(string documentId, 
    string asyncExportOperationId, string format, ExportOptions options, 
    PrintingSystemBase printingSystem, Func<ExportedDocument> doExportSynchronously)
    {
        switch (format)
        {
            case "JPEG":
                {
                    var report = new TestReport();
                    byte[] bytes;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        report.ExportToImage(ms,
                            new ImageExportOptions()
                            {
                                Format = ImageFormat.Jpeg,
                                ExportMode = ImageExportMode.SingleFilePageByPage,
                                PageRange = "1"
                            });
                        bytes = ms.ToArray();
                    }
                    return new ExportedDocument(bytes, @"image/jpeg", "inline", "SampleImage.jpeg");
                }
            default: return base.ExportDocumentStarting(documentId, 
            asyncExportOperationId, format, options, printingSystem, doExportSynchronously);
        }
    }
}

Example: Add a Comment to the Document Exported to XLSX Format

The following code adds comments to the cells of the exported XLSX spreadsheet.

using DevExpress.Spreadsheet;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.Web.ClientControls;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using System;
using System.IO;

public class MyOperationLogger : WebDocumentViewerOperationLogger
{
    public override ExportedDocument ExportDocumentStarting(string documentId, 
        string asyncExportOperationId, string format, 
        ExportOptions options, PrintingSystemBase printingSystem, 
        Func<ExportedDocument> doExportSynchronously)
    {
        // ...
        if (format == "xls" || format == "xlsx")
        {
            // Calls the default Export procedure.
            ExportedDocument document = doExportSynchronously();

            // Transforms the exported document to a Workbook class instance.  
            IWorkbook workbook = new Workbook();
            workbook.LoadDocument(document.Bytes);
            Worksheet worksheet = workbook.Worksheets[0];

            // Gets the username.  
            string author = workbook.CurrentAuthor;

            // Adds a comment to the A1 cell.  
            Cell cell = worksheet.Cells["A1"];
            Comment comment = worksheet.Comments.Add(cell, author, 
                "This is important information for users.");

            // Adds a user name to beginning of the comment.  
            CommentRunCollection runs = comment.Runs;
            runs.Insert(0, author + ": \r\n");
            runs[0].Font.Bold = true;

            // Formats the comment text.  
            runs[1].Font.Color = System.Drawing.Color.Red;
            runs[1].Font.Name = "Times New Roman";
            runs[1].Font.Size = 14;
            runs[1].Font.Italic = true;

            // Adds a new comment to the document collection of 'runs'.  
            runs.Add("\n Never delete this comment!");
            runs[2].Font.Color = System.Drawing.Color.MidnightBlue;

            // Saves the modified document.  
            MemoryStream ms = new MemoryStream();
            workbook.SaveDocument(ms, (format == "xls") ? DocumentFormat.Xls : DocumentFormat.Xlsx);
            document.Bytes = ms.ToArray();
            return document;
        }
        return base.ExportDocumentStarting(documentId, 
            asyncExportOperationId, format, options, 
            printingSystem, doExportSynchronously);
    }
}

Note

The Workbook class is defined in the DevExpress.Docs.v24.1.dll assembly. Add this assembly or install the DevExpress.Document.Processor NuGet package to your project to use the Workbook API. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this assembly in production code.

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ExportDocumentStarting(String, String, String, ExportOptions, PrintingSystemBase, Func<ExportedDocument>) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also