Skip to main content
All docs
V23.2

Print and Export Gantt Control

  • 7 minutes to read

The XtraPrinting library allows you to print and export a project from a GanttControl.

The ShowRibbonPrintPreview() method invokes the Print Preview window that allows a user to do the following:

  • Select the project’s visual elements that should be printed.
  • Set the page size, orientation, and margins.
  • Navigate through the preview of the printed project.
  • Select the printer, set the number of copies, and print the project.

Gantt Control Print Preview

Note

For more accurate output, certain visual elements (for example, rounded borders) are simplified when the project is printed or exported.

Chart area can be exported only to PDF or raster image formats. To export Gantt data to other formats (without the chart area), use GanttControl.ExportTo... methods.

Commands in the window are organized in a ribbon. You can also call the ShowPrintPreview() method that invokes the Print Preview window where commands are organized in bars.

Call the Print() method to print the control immediately, without displaying the Print Preview window.

Note

You can only print controls if the application references the XtraPrinting library. Use the IsPrintingAvailable property to check if you can print the control.

Example

The code below shows how to display a notification if the control cannot be printed. To check whether it is possible to print the control, the code uses the IsPrintingAvailable property. The example also uses the following methods:

using DevExpress.XtraGanttt;
using DevExpress.XtraEditors;

private void ShowPrintPreview(GanttControl ganttControl) {
    if (!ganttControl.IsPrintingAvailable) {
        XtraMessageBox.Show("XtraPrint library not found", "Error");
        return;
    }
    ganttControl.ShowRibbonPrintPreview();
}

private void Print(GanttControl ganttControl) {
    if (!ganttControl.IsPrintingAvailable) {
        XtraMessageBox.Show("XtraPrint library not found", "Error");
        return;
    }
    ganttControl.Print();
}

You can also use the IsPrinting property to determine whether the control is already being printed.

The Print Options command in the Print Preview invokes a window that allows a user to specify visual elements that should be printed. You can use the OptionsPrint property to access these properties in code. For example, you can specify whether or not to print column headers, grid lines, the chart legend, and so on.

Legend

Enable the PrintLegend property to display the chart legend in page footers. The image below illustrates the default chart legend.

Gantt Control Legend

The AppearancePrint property provides access to appearance settings used to print the control. You can use the following properties to specify how to print the legend:

  • Legend — appearance settings used to print the chart legend.
  • LegendItem — appearance settings used to print items in the chart legend.

Ensure that the UsePrintStyles property is set to true (the default value) to enable these settings.

Example

The code below specifies print options and invokes the print preview.

ganttControl1.OptionsPrint.PrintLegend = true;
ganttControl1.ShowRibbonPrintPreview();

Page Information

Enable the PrintPageInfo property to display additional information in page footers. The default footer contains the current date, page number, and the total number of pages.

Gantt Control Page Information

Use the PageInfoType property to specify the type of information that should be displayed. The PageInfoFormat property allows you to apply a custom format to this information. Use the {0} and {1} placeholders to insert a specific portion of information into the format. The passed content depends on the type of information. For example, if PageInfoType is set to NumberOfTotal, {0} is replaced with the page number, and {1} is replaced with the total number of pages.

You can also use the PageInfoCaption property to display a custom caption in the page footers.

The AppearancePrint property provides access to the PageInfo and PageInfoCaption properties that specify appearance settings used to print page information.

Example

The code below shows how to display the chart legend and page information.

Gantt Control Custom Legend

using System.Drawing;
using DevExpress.XtraPrinting;

// Print the chart legend.
ganttControl1.OptionsPrint.PrintLegend = DefaultBoolean.True;
// Specify custom foreground and background colors for the chart legend.
ganttControl1.AppearancePrint.Legend.BackColor = Color.FromArgb(224, 224, 224);
ganttControl1.AppearancePrint.Legend.Options.UseBackColor = true;
ganttControl1.AppearancePrint.LegendItem.ForeColor = Color.FromArgb(64, 0, 64);
ganttControl1.AppearancePrint.LegendItem.Options.UseForeColor = true;
// Print the Project's caption and Project Manager's name in the footer.
ganttControl1.OptionsPrint.PrintPageInfo = DefaultBoolean.True;
ganttControl1.OptionsPrint.PageInfoType = XtraPrinting.PageInfo.UserName;
ganttControl1.OptionsPrint.PageInfoFormat = "Project Manager: {0}";
ganttControl1.OptionsPrint.PageCaption = "Software Development";
// Specify the footer background color.
ganttControl1.AppearancePrint.PageInfo.BackColor = Color.FromArgb(255, 192, 128);
ganttControl1.AppearancePrint.PageInfo.Options.UseBackColor = true;

Project Start and Finish Dates

The control’s ChartStartDate and ChartFinishDate properties specify the first and last visible dates in the chart area when it is displayed on screen.

You can also use the OptionsPrint property to access the ChartStartDate and ChartFinishDate properties that specify the first and last visible dates when the chart is printed or exported.

Example

The code below prints the project’s following month.

ganttControl1.OptionsPrint.ChartStartDate = DateTime.Now;
ganttControl1.OptionsPrint.ChartFinishDate = DateTime.Now.AddDays(30);
ganttControl1.Print();

Localization

You can use satellite assemblies or GanttLocalizer to translate captions displayed in the Print Preview and other windows. See the following topic for more information: Localization.

Advanced Settings

You can also use the PrintableComponentLink to print the control. The link provides access to advanced settings, such as paper size and margins, custom headers and footers, and so forth. See the following topic for an example: How to: Set Paper Format and Add Custom Information to the Report when Printing/Exporting a Control.

Handle Print Events

Before a project is printed or exported, the control raises the following events that allow you to draw visual elements in a custom way:

  • CustomPrintTask—allows you to draw a Gantt bar. This event is equivalent to the CustomDrawTask event, which allows you to customize a Gantt bar when it is displayed onscreen.

  • CustomPrintTaskDependency—allows you to draw a dependency line. This event is equivalent to the CustomDrawTaskDependency event, which allows you to customize a dependency line when it is displayed onscreen.

  • CustomPrintTimescaleColumn—allows you to draw a timescale column (the header, which displays the date, and the chart area). This event is equivalent to the CustomDrawTimescaleColumn event, which allows you to customize a timescale column when it is displayed onscreen.

Example

The code below shows how to highlight specific tasks and dependencies when the project is printed.

Gantt Control Custom Print Task

Run Demo: Highlight Tasks and Dependencies

HashSet<int> tasks = new HashSet<int> { 1, 2, 3, 6, 7, 8, 10, 11, 13 };
ganttControl.CustomPrintTask += (sender, e) => {
    int taskId = Convert.ToInt32(e.Node.GetValue("Id"));
    if(tasks.Contains(taskId)) {
        e.Appearance.BackColor = DXSkinColors.FillColors.Warning;
        e.Appearance.ProgressColor = DXSkinColors.FillColors.Warning;
    }
};
ganttControl.CustomPrintTaskDependency += (sender, e) => {
    int predecessorId = Convert.ToInt32(e.PredecessorNode.GetValue("Id"));
    int successorId = Convert.ToInt32(e.SuccessorNode.GetValue("Id"));
    if(tasks.Contains(predecessorId) && tasks.Contains(successorId)) {
        e.Appearance.BackColor = DXSkinColors.FillColors.Warning;
    }
};
See Also