Search results

Pdf Export in ASP.NET MVC Gantt control

15 Oct 2021 / 4 minutes to read

PDF export allows exporting Gantt data to PDF document. You need to use the pdfExport method for exporting. To enable PDF export in the Gantt, set the allowPdfExport to true.

To export data to PDF document, inject the PdfExport module in Gantt.

Note: Currently, we don’t have support for exporting the manually scheduled tasks.

razor
pdfexport.cs
Copied to clipboard
@using Syncfusion.EJ2
@{
    ViewBag.Title = "DefaultFunctionalities";
}
<div class="container body-content">
    @(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
            "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Toolbar(new List<string>() {
     "PdfExport" }).ToolbarClick("toolbarClick").Render())

    <script>
    function toolbarClick(args) {
        var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
        if (args.item.id === "GanttContainer_pdfexport") {
            ganttObj.pdfExport();
        }
    }
    </script>
</div>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Multiple exporting

PDF export provides an option for exporting multiple Gantt to same file. In this exported document, each Gantt will be exported to a new page of the document in same file.

razor
pdfmultipleexport.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer1").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("280px").AllowPdfExport(true).TaskFields(ts =>
     ts.Id("TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Toolbar(new List<string>() {
"PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).ProjectStartDate("03/31/2019").ProjectEndDate("04/14/2019").Render())

@(Html.EJS().Gantt("GanttContainer2").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("250px").AllowPdfExport(true).TaskFields(ts =>
     ts.Id("TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Toolbar(new List<string>() {
"PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).Render())

<script>
    function toolbarClick(args) {
        var firstGantt = document.getElementById("GanttContainer1").ej2_instances[0];
        var secondGantt = document.getElementById("GanttContainer2").ej2_instances[0];
        if (args.item.id === 'GanttContainer1_pdfexport') {
            var firstGanttExport = firstGantt.pdfExport({}, true);
            firstGanttExport.then((fData) => {
                secondGantt.pdfExport({}, false, fData);
            });
        }
    }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

To customize PDF export

PDF export provides an option to customize the mapping of Gantt to exported PDF document.

File name for exported document

You can assign a file name for the exported document by defining the fileName property in pdfExportProperties.

razor
file-name.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
        "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Toolbar(new List<string>() {
"PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).Render())

<script>
    function toolbarClick(args) {
        var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
        if (args.item.id === "GanttContainer_pdfexport") {
            var exportProperties = {
                fileName: "new.pdf"
            };
            ganttObj.pdfExport(exportProperties);
        }
    }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

How to change page orientation

Page orientation can be changed to Portrait (Default Landscape) for the exported document using the property pdfExportProperties.pageOrientation.

razor
page-orientation.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
        "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Toolbar(new List<string>() {
"PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).Render())

<script>
    function toolbarClick(args) {
        var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
        if (args.item.id === "GanttContainer_pdfexport") {
            var exportProperties = {
                pageOrientation: 'Portrait'
            };
            ganttObj.pdfExport(exportProperties);
        }
    }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

How to change page size

Page size can be customized for the exported document using the property pdfExportProperties.pageSize. The supported page sizes are:

  • Letter
  • Note
  • Legal
  • A0
  • A1
  • A2
  • A3
  • A5
  • A6
  • A7
  • A8
  • A9
  • B0
  • B1
  • B2
  • B3
  • B4
  • B5
  • Archa
  • Archb
  • Archc
  • Archd
  • Arche
  • Flsa
  • HalfLetter
  • Letter11x17
  • Ledger
razor
page-size.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
        "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Toolbar(new List<string>() {
"PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).Render())

<script>
    function toolbarClick(args) {
        var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
        if (args.item.id === "GanttContainer_pdfexport") {
            var exportProperties = {
                pageSize: 'A0'
            };
            ganttObj.pdfExport(exportProperties);
        }
    }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Export current view data

PDF export provides an option to export the current view data into PDF. To export current view data alone, define the exportType to CurrentViewData.

razor
current-view-data.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
        "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Toolbar(new List<string>() {
"PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).Render())

<script>
    function toolbarClick(args) {
        var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
        if (args.item.id === "GanttContainer_pdfexport") {
            var exportProperties = {
                exportType: 'CurrentViewData'
            };
            ganttObj.pdfExport(exportProperties);
        }
    }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

By default, we render the default footer for a PDF file, this can be enabled or disabled by using the enableFooter property.

razor
footer.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
        "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Toolbar(new List<string>() {
"PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).Render())

<script>
    function toolbarClick(args) {
        var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
        if (args.item.id === "GanttContainer_pdfexport") {
            var exportProperties = {
                enableFooter: false
            };
            ganttObj.pdfExport(exportProperties);
        }
    }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Export hidden columns

PDF export provides an option to export hidden columns of Gantt by defining the includeHiddenColumn to true.

hidden-columns.cs
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Export predecessor lines

By using showPredecessorLines, you can hide or show predecessor lines in the exported PDF document.

razor
connector-lines.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>
    )ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
    "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Columns(col =>
    {
        col.Field("TaskId").Width(100).HeaderText("Task Id").Add();
        col.Field("TaskName").HeaderText("Task Name").Width(150).Add();
        col.Field("StartDate").HeaderText("StartDate").Width(150).Visible(false).Add();
        col.Field("Duration").Width(150).HeaderText("Duration").Add();
        col.Field("Progress").HeaderText("Progress").Width(150).Add();
    }).Toolbar(new List<string>
        () {
        "PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).Render())

        <script>
            function toolbarClick(args) {
                var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
                if (args.item.id === "GanttContainer_pdfexport") {
                    var exportProperties = {
                        showPredecessorLines: true
                    };
                    ganttObj.pdfExport(exportProperties);
                }
            }
        </script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Show or hide columns on exported PDF

You can show a hidden column or hide a visible column while exporting the Gantt using the toolbarClick and beforePdfExport events.

You can show or hide columns by setting the column.visible property to true or false respectively.

In the following example, there is a hidden column Duration in the Gantt. While exporting, we have changed Duration to visible column and StartDate to hidden column.

razor
showhide-columns.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>
    )ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
    "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Columns(col =>
    {
        col.Field("TaskId").Width(100).HeaderText("Task Id").Add();
        col.Field("TaskName").HeaderText("Task Name").Width(150).Add();
        col.Field("StartDate").HeaderText("StartDate").Width(150).Add();
        col.Field("Duration").Width(150).HeaderText("Duration").Visible(false).Add();
        col.Field("Progress").HeaderText("Progress").Width(150).Add();
    }).Toolbar(new List<string>
    () {"PdfExport" }).ToolbarClick("toolbarClick").BeforePdfExport("beforePdfExport").TreeColumnIndex(1).Render())

    <script>
        function toolbarClick(args) {
            var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
            if (args.item.id === "GanttContainer_pdfexport") {
                ganttObj.pdfExport();
            }
        }
        function beforePdfExport() {
            var obj = (document.getElementById('GanttContainer')).ej2_instances[0]
            obj.treeGrid.columns[3].visible = true;
            obj.treeGrid.columns[2].visible = false;
        }


    </script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Conditional cell formatting

TreeGrid cells in the exported PDF can be customized or formatted using the pdfQueryCellInfo event. In this event, you can format the treegrid cells of exported PDF document based on the column cell value.

In the following sample, the background color is set for Progress column in the exported document by using the args.style and backgroundColor properties.

razor
cell-customization.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>
    )ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
    "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Columns(col =>
    {
        col.Field("TaskId").Width(100).HeaderText("Task Id").Add();
        col.Field("TaskName").HeaderText("Task Name").Width(150).Add();
        col.Field("StartDate").HeaderText("StartDate").Width(150).Add();
        col.Field("Duration").Width(150).HeaderText("Duration").Visible(false).Add();
        col.Field("Progress").HeaderText("Progress").Width(150).Add();
    }).Toolbar(new List<string>
    () {"PdfExport" }).ToolbarClick("toolbarClick").PdfQueryCellInfo("pdfQueryCellInfo").Render())

    <script>
        function toolbarClick(args) {
            var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
            if (args.item.id === "GanttContainer_pdfexport") {
                ganttObj.pdfExport();
            }
        }
        function pdfQueryCellInfo(args){
            if (args.column.field == 'Progress') {
                if (args.value < 50) {
                    args.style.backgroundColor = new ej.pdfexport.PdfColor(240, 128, 128);
                } else {
                    args.style.backgroundColor = new ej.pdfexport.PdfColor(205, 92, 92);
                }
            }
        }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Timeline cell formatting

Timeline cells in the exported PDF document can be customized or formatted using the pdfQueryTimelineCellInfo event.

In the following sample, the header background color is set for timeline cells in the exported document by using the args.headerBackgroundColor property.

razor
timeline-customization.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>
    )ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
    "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Columns(col =>
    {
        col.Field("TaskId").Width(100).HeaderText("Task Id").Add();
        col.Field("TaskName").HeaderText("Task Name").Width(150).Add();
        col.Field("StartDate").HeaderText("StartDate").Width(150).Add();
        col.Field("Duration").Width(150).HeaderText("Duration").Visible(false).Add();
        col.Field("Progress").HeaderText("Progress").Width(150).Add();
    }).Toolbar(new List<string>
    () {"PdfExport" }).ToolbarClick("toolbarClick").PdfQueryTimelineCellInfo("pdfQueryTimelineCellInfo").Render())

    <script>
        function toolbarClick(args) {
            var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
            if (args.item.id === "GanttContainer_pdfexport") {
                ganttObj.pdfExport();
            }
        }
        function pdfQueryTimelineCellInfo(args) {
            args.timelineCell.backgroundColor = new ej.pdfexport.PdfColor(240, 248, 255);
        }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Taskbar formatting

Taskbars in the exported PDF document can be customized or formatted using the pdfQueryTaskbarInfo event.

In the following sample, the taskbar background color is customized in the chart side of the exported document by using the args.taskbar property.

razor
taskbar-customization.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>
    )ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
    "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Columns(col =>
    {
        col.Field("TaskId").Width(100).HeaderText("Task Id").Add();
        col.Field("TaskName").HeaderText("Task Name").Width(150).Add();
        col.Field("StartDate").HeaderText("StartDate").Width(150).Add();
        col.Field("Duration").Width(150).HeaderText("Duration").Visible(false).Add();
        col.Field("Progress").HeaderText("Progress").Width(150).Add();
    }).Toolbar(new List<string>
    () {"PdfExport" }).ToolbarClick("toolbarClick").PdfQueryTaskbarInfo("pdfQueryTaskbarInfo").Render())

    <script>
        function toolbarClick(args) {
            var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
            if (args.item.id === "GanttContainer_pdfexport") {
                ganttObj.pdfExport();
            }
        }
        function pdfQueryTaskbarInfo(args) {
            if (args.data.Progress < 50 && !args.data.hasChildRecords) {
                args.taskbar.progressColor = new ej.pdfexport.PdfColor(205, 92, 92);
                args.taskbar.taskColor = args.taskbar.taskBorderColor = new ej.pdfexport.PdfColor(240, 128, 128);
            }
        }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Theme

PDF export provides an option to include theme for the exported PDF document. To apply theme in exported PDF, define the theme in pdfExportProperties. The available themes are:

  • Material
  • Fabric
  • Bootstrap
  • Bootstrap 4
razor
themes.cs
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>
    )ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
    "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Columns(col =>
    {
        col.Field("TaskId").Width(100).HeaderText("Task Id").Add();
        col.Field("TaskName").HeaderText("Task Name").Width(150).Add();
        col.Field("StartDate").HeaderText("StartDate").Width(150).Add();
        col.Field("Duration").Width(150).HeaderText("Duration").Visible(false).Add();
        col.Field("Progress").HeaderText("Progress").Width(150).Add();
    }).Toolbar(new List<string>
    () {
    "PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).Render())

    <script>
        function toolbarClick(args) {
            var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
            if (args.item.id === "GanttContainer_pdfexport") {
                var exportProperties = {
                    theme: "Fabric" 
                };
                ganttObj.pdfExport(exportProperties);
            }
        }
        
    </script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Customized Theme

PDF export provides an option to customize the Gantt style for the exported PDF document. To customize Gantt style in exported PDF, define the ganttStyle in pdfExportProperties.

razor
Copied to clipboard
@(Html.EJS().Gantt("GanttContainer").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPdfExport(true).TaskFields(ts => ts.Id(
        "TaskId").Name("TaskName").StartDate("StartDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Toolbar(new List<string>() {
"PdfExport" }).ToolbarClick("toolbarClick").TreeColumnIndex(1).Render())

<script>
    function toolbarClick(args) {
        var ganttObj = document.getElementById("GanttContainer").ej2_instances[0];
        if (args.item.id === "GanttContainer_pdfexport") {
            var exportProperties = {
                fontFamily: 1,
                columnHeader: {
                    backgroundColor: new ej.pdfexport.PdfColor(179, 219, 255)
                },
                taskbar: {
                    taskColor: new ej.pdfexport.PdfColor(240, 128, 128),
                    taskBorderColor: new ej.pdfexport.PdfColor(240, 128, 128),
                    progressColor: new ej.pdfexport.PdfColor(205, 92, 92),
                },
                connectorLineColor: new ej.pdfexport.PdfColor(128, 0, 0),
                footer: {
                    backgroundColor: new ej.pdfexport.PdfColor(205, 92, 92)
                },
                timeline: {
                    backgroundColor: new ej.pdfexport.PdfColor(179, 219, 255),
                    //padding: new PdfPaddings(5, 2, 0, 0),
                },
                label: {
                    fontColor: new ej.pdfexport.PdfColor(128, 0, 0),
                },
                cell: {
                    backgroundColor: new ej.pdfexport.PdfColor(240, 248, 255),
                    fontColor: new ej.pdfexport.PdfColor(0, 0, 0),
                    borderColor: new ej.pdfexport.PdfColor(179, 219, 255),
                },
            };
            ganttObj.pdfExport(exportProperties);
        }
    }
</script>