windowsforms-401786-controls-and-libraries-gantt-control-print-export.md
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:
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.
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:
ShowRibbonPrintPreview() — to show the print preview.
Print() — to print the control.
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();
}
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGantt
Private Sub ShowPrintPreview(ByVal ganttControl As GanttControl)
If Not ganttControl.IsPrintingAvailable Then
XtraMessageBox.Show("XtraPrint library not found", "Error")
Return
End If
ganttControl.ShowRibbonPrintPreview()
End Sub
Private Sub Print(ByVal ganttControl As GanttControl)
If Not ganttControl.IsPrintingAvailable Then
XtraMessageBox.Show("XtraPrint library not found", "Error")
Return
End If
ganttControl.Print()
End Sub
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.
Enable the PrintLegend property to display the chart legend in page footers. The image below illustrates the default chart 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:
Ensure that the UsePrintStyles property is set to true (the default value) to enable these settings.
The code below specifies print options and invokes the print preview.
ganttControl1.OptionsPrint.PrintLegend = true;
ganttControl1.ShowRibbonPrintPreview();
ganttControl1.OptionsPrint.PrintLegend = True
ganttControl1.ShowRibbonPrintPreview()
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.
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.
The code below shows how to display the chart legend and page information.
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;
Imports System.Drawing
Imports 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
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.
The code below prints the project’s following month.
ganttControl1.OptionsPrint.ChartStartDate = DateTime.Now;
ganttControl1.OptionsPrint.ChartFinishDate = DateTime.Now.AddDays(30);
ganttControl1.Print();
ganttControl1.OptionsPrint.ChartStartDate = DateTime.Now
ganttControl1.OptionsPrint.ChartFinishDate = DateTime.Now.AddDays(30)
ganttControl1.Print()
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.
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.
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.
The code below shows how to highlight specific tasks and dependencies when the project is printed.
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;
}
};
Dim tasks As New HashSet(Of Integer)() From {1, 2, 3, 6, 7, 8, 10, 11, 13}
AddHandler ganttControl.CustomPrintTask, Sub(sender, e)
Dim taskId As Integer = Convert.ToInt32(e.Node.GetValue("Id"))
If tasks.Contains(taskId) Then
e.Appearance.BackColor = DXSkinColors.FillColors.Warning
e.Appearance.ProgressColor = DXSkinColors.FillColors.Warning
End If
End Sub
AddHandler ganttControl.CustomPrintTaskDependency, Sub(sender, e)
Dim predecessorId As Integer = Convert.ToInt32(e.PredecessorNode.GetValue("Id"))
Dim successorId As Integer = Convert.ToInt32(e.SuccessorNode.GetValue("Id"))
If tasks.Contains(predecessorId) AndAlso tasks.Contains(successorId) Then
e.Appearance.BackColor = DXSkinColors.FillColors.Warning
End If
End Sub
See Also