Blogs

Blogs

DevExpress XAF – Printing & Exporting Record Using Custom Actions and Templates

Scenario

Within the DevExpress application we can create records such as Invoices, Purchase Orders or Credit Notes. We required the ability to print and/or export these using a predefined template so that it will be simple for a user to click a single action from within the DevExpress application and the Invoice, Purchase Order or Credit Note will be generated using the relevant template and printed, following the same consistent style each time. 

Solution

To implement this functionality, first of all it is necessary to create the reports which you wish to use as the templates, it may be necessary to also create sub reports to show the children of the parent report. All reports will need a single parameter which will be the record which you pass through to the template before printing or exporting, to pass a parameter though from a parent report to a sub report please see the blog post: DevExpress XtraReports – Filtering Report Via Script

Once you have completed the reports you will need to create a view controller and assign it to the detail view of the record type which you wish to print and export. Within the view controller, add two simple actions, Print and Export. Configure these buttons accordingly within the designer by assigning tooltips, captions, images and target object criteria.

Once you have created the action buttons you have to create the executing event for the buttons, the below code example shows, how to setup the executing events for printing and exporting to PDF.

/// <summary>
/// Handles the Execute event of the Print control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="SimpleActionExecuteEventArgs"/> instance containing the event data.</param>
private void Print_Execute(object sender, SimpleActionExecuteEventArgs e)
{
    Record record = (Record)((DevExpress.ExpressApp.DetailView)this.ObjectSpace.Owner).CurrentObject;
    IReportData reportData = ObjectSpace.FindObject<ReportData>(new BinaryOperator("Name", "Print Template"));
 
    if (reportData != null)
    {
        IObjectSpace objectSpace = Application.CreateObjectSpace(reportData.GetType());
 
        DevExpress.XtraReports.UI.XtraReport report = reportData.LoadReport(objectSpace);
        report.Parameters["Record"].Value = record;
 
        ReportPrintTool printTool = new ReportPrintTool(report);
        printTool.AutoShowParametersPanel = false;
        printTool.PrintDialog();
    }
}
 
/// <summary>
/// Handles the Execute event of the Export control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="SimpleActionExecuteEventArgs"/> instance containing the event data.</param>
private void Export_Execute(object sender, SimpleActionExecuteEventArgs e)
{
    Record record = (Record)((DevExpress.ExpressApp.DetailView)this.ObjectSpace.Owner).CurrentObject;
    IReportData reportData = ObjectSpace.FindObject<ReportData>(new BinaryOperator("Name", "Export Template"));
 
    if (reportData != null)
    {
        IObjectSpace objectSpace = Application.CreateObjectSpace(reportData.GetType());
 
        DevExpress.XtraReports.UI.XtraReport report = reportData.LoadReport(objectSpace);
        report.Parameters["Record"].Value = record;
 
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.Filter = "PDF|*.pdf";
        saveFileDialog.DefaultExt = ".pdf";
        saveFileDialog.FileName = string.Concat("Exported Template - ", record.ReferenceNumber, ".pdf");
        saveFileDialog.ShowDialog();
 
        using (FileStream fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create))
        {
            report.ExportToPdf(fileStream);
        }
    }
}

Once you have completed the executing events it will be necessary to assign the events to the buttons you created previously.

this.printButton.Execute += new DevExpress.ExpressApp.Actions.SimpleActionExecuteEventHandler(this.Print_Execute);

Within the print executing action you can call PrintTool.ShowPreview() instead of PrintTool.PrintDialog() to show a print preview on screen of the completed template before it is printed.

This website uses cookies to provide the best possible experience. By continuing to use our website you are agreeing to our use of cookies.