Export table and chart into the same document using toolbar

4 Aug 20231 minute to read

Even if the PivotViewDisplayOption.View property is set to Both in the pivot table, you can only export either the table or the chart to the PDF document based on the current value set in the PivotViewDisplayOption.Primary property. But, to export both the table and the chart to the same PDF document, use the pdfExport method during the ActionBegin event invoke.

In the following example, the built-in export action can be restricted by setting the args.cancel option to true in the ActionBegin event, and both the table and the chart can be exported by calling the pdfExport method and setting the exportBothTableAndChart argument to true.

@using Syncfusion.EJ2.PivotView

@Html.EJS().PivotView("pivotview").Height("300").ShowToolbar(true).ShowFieldList(true).AllowPdfExport(true).DataSourceSettings(dataSourceSettings => dataSourceSettings.DataSource((IEnumerable<object>)ViewBag.DataSource).ExpandAll(false)
.FormatSettings(formatsettings =>
{
    formatsettings.Name("Amount").Format("C0").Add();
}).Rows(rows =>
{
    rows.Name("Country").Add(); rows.Name("Products").Add();
}).Columns(columns =>
{
    columns.Name("Year").Caption("Year").Add(); columns.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).GridSettings(new Syncfusion.EJ2.PivotView.PivotViewGridSettings { ColumnWidth = 140 }).DisplayOption(new PivotViewDisplayOption { View = Syncfusion.EJ2.PivotView.View.Both }).Toolbar(new List<string>
    () { "Grid", "Chart", "Export", "FieldList" }).ActionBegin("actionBegin").Render()
<script>
    function actionBegin(args) {
        var pivotTableObj = document.getElementById('pivotview').ej2_instances[0];
        if (args.actionName == 'PDF export') {
            args.cancel = true;
            pivotTableObj.pdfExport({}, false, null, false, true);
        }
    }
</script>
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.DataSource = data;
    return View();
}