Xceed Chart for WinForms v4.4 Documentation
Printing Charts

Welcome to Xceed Chart for WinForms v4.4 > User Guide > Printing > Printing Charts

This topic describes a simple scenario: when you have to print a chart on a printer. For this purpose you may use the PrintManager object of the control. The print manager is accessible through the PrintManager property of the control:

VB.NET  
Dim printManager As PrintManager =   ChartControl.PrintManager

C#  
PrintManager printManager = ChartControl.PrintManager;

As with most objects in Xceed Chart, the PrintManager object is also configured with some default settings, so you may directly execute the print method of the object that will print a page with default settings on your default printer:

VB.NET  

Dim printManager As PrintManager = ChartControl.PrintManager
printManager.Print()
C#  
PrintManager printManager = ChartControl.PrintManager;
printManager.Print();

In practice you'll have to modify some of the properties of the print manager. There are two major strategies you may use when it comes to printing with the built-in features of the control. The first one is to let the control configure the print document. In this case all you have to do is modify the desired properties of the print manager. The second strategy is more sophisticated but more flexible in terms of the control you have over the printing process. In this case you pass a prepared print document to the chart and receive a bitmap that is properly rendered to print on that document. You are responsible for actually printing the chart on the printer device. The following paragraphs describe each of these strategies.

Printing without Specifying a Print Document

In this mode the control will automatically create a print document for you and configure it according to the settings of the print manager object.

Print Margins

You modify the print margins by changing the value of the Margins property:

VB.NET  
ChartControl.PrintManager.Margins = New Margins(200, 200, 200, 200)

C#  
ChartControl.PrintManager.Margins = new Margins(200, 200, 200, 200);

Note that margins are specified in hundredths of an inch, so the code above tells the chart to print two inches from the edge of the sheet.

Number of Copies

You control the number of copies printed by changing the value of the Copies property:

VB.NET  
ChartControl.PrintManager.Copies = 2 ' I want to print two copies

C#  
ChartControl.PrintManager.Copies = 2; // I want to print two copies

By default the chart will print one copy.

Paper Orientation

Paper orientation is controlled via the Landscape property. When set to true, the control will print in landscape. Otherwise, it will use portrait orientation.

VB.NET  
ChartControl.PrintManager.Landscape = True ' I want to print a landscape image

C#  
ChartControl.PrintManager.Landscape = true; // I want to print a landscape image

Grayscale

The PrintGrayScale property defines whether the control must convert the generated chart image to black and white (grayscale) before printing. You may use this feature on black and white printers or on printers where you print in color mode but would like the image to appear in black and white, or if you simply want to save your color cartridge. 

The grayscale conversion is performed by calculating the intensity of each pixel in the rendered chart, so different colors may look the same when printed in grayscale mode. This is why it is recommended to use patterns for such types of printing. The following code enables grayscaling:

VB.NET  
ChartControl.PrintManager.PrintGrayScale = True

C#  
ChartControl.PrintManager.PrintGrayScale = true;

Printing the Background Frame

By default when you print the frame applied to the chart, the background is not rendered. This is because most likely you'll print with a white chart background and the frame will not merge properly with the page background, as you'll see a rectangle around the printed chart. In some cases however this may be the desired behavior, for example, when you want to print a chart with an image border around it. To include the background frame in the printed chart, you must enable the PrintBackgroundFrame property:

VB.NET  
ChartControl.PrintManager.PrintBackgroundFrame = True

C#  
ChartControl.PrintManager.PrintBackgroundFrame = true;

Scaling

Most printers have a higher resolution than monitors. For this reason, programs that do not take this into account will print poor-looking images, even though the image looks fine on a monitor. Xceed Chart for WinForms can detect the target printer's resolution and automatically scale the rendered image so that there is no loss of quality. The AutomaticQualityScale property controls whether the chart should internally scale the image. This feature is enabled by default. In cases where you want to manually specify the scaling factor, you must turn off this property and adjust scaling from the QualityScale property:

VB.NET  

ChartControl.PrintManager.AutomaticQualityScale = False
ChartControl.PrintManager.QualityScale = 2 ' scale the printed image twice
C#  
ChartControl.PrintManager.AutomaticQualityScale = false;
ChartControl.PrintManager.QualityScale = 2; // scale the printed image twice

Scaling 2D objects

Besides scaling the whole image you have control over the scale of the 2D objects in the chart. These are all objects whose size is specified in pixels (or device units), for example, labels, fonts, legend text offset, etc. If you want to scale these objects when printing, you must set the Scale2DObjects property to a value greater than 1:

VB.NET  
ChartControl.PrintManager.Scale2DObjects = 2

C#  
ChartControl.PrintManager.Scale2DObjects = 2;

Note that this scale is cumulative with the quality scale, which is applied on all chart elements. 

To summarize, the following code will print a grayscale chart on your printer, two inches from the edge of the sheet, in landscape mode, and the 2D objects will be two times larger than they appear on the screen:

VB.NET  

ChartControl.PrintManager.Margins = New Margins(200, 200, 200, 200)
ChartControl.PrintManager.Landscape = True ' I want to print a landscape image
ChartControl.PrintManager.PrintGrayScale = True
ChartControl.PrintManager.PrintBackgroundFrame = False
ChartControl.PrintManager.AutomaticQualityScale = True
ChartControl.PrintManager.Scale2DObjects = 2
ChartControl.PrintManager.Print()
C#  
ChartControl.PrintManager.Margins = new Margins( 200, 200, 200, 200);
ChartControl.PrintManager.Landscape = true; // I want to print a landscape image
ChartControl.PrintManager.PrintGrayScale = true;
ChartControl.PrintManager.PrintBackgroundFrame = false;
ChartControl.PrintManager.AutomaticQualityScale = true;
ChartControl.PrintManager.Scale2DObjects = 2;
ChartControl.PrintManager.Print();

Print Events

The print manager also fires several events that help you monitor the printing process. The first event is called PrepareDocument and is fired before the print job is started. This event allows you to additionally modify the print document and override the settings applied by the print manager. 

The second event is BeforePrintPage. It is fired before the chart draws the chart image on the printer device. You may wish to intercept this event if you want to modify the chart's appearance before the chart is printed. This is very useful if you have to print multiple copies that differ in some way. Typical code for such an event might look like this:

VB.NET  

Public Sub OnPrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
' chart modifications go here
......
' end chart modifications

' update the cached image for printing
ChartControl.PrintManager.UpdateChartImage(CType(sender, PrintDocument))
End Sub
C#  

public void OnPrintPage(object sender, PrintPageEventArgs e)
{
// chart modifications go here
......
// end chart modifications
// update the cached image for printing
ChartControl.PrintManager.UpdateChartImage((PrintDocument)sender);
}

Advanced Printing

In the previous sections, we discussed a simple printing scenario where you did not have to worry about creating a print document and intercepting print page events. In practice however you'll most likely need to familiarize yourself with this if you want to print pages containing other information than charts. For example, let's say that you must print a page containing text and a chart: 

 

Because a complete discussion on printing with .NET is beyond the scope of this topic, we'll simply outline the steps required to accomplish this: 

1. Prepare a print document, initialize it, and pass it to the RenderToBitmap method of the Print Manager:

VB.NET  

Dim printDocument As PrintDocument = New PrintDocument()
Dim chartBitmap As Bitmap = m_PrintManager.RenderToBitmap(printDocument)
C#  
PrintDocument printDocument = new PrintDocument();
Bitmap chartBitmap = m_PrintManager.RenderToBitmap(printDocument);

2. Now you have a bitmap of the chart that the print manager prepared for you using the settings of the PrintManager object. You don't have to worry about scaling and grayscaling: all that matters is that you have the image. Note that if you do not explicitly specify margins to the print manager, the chart will use the default PrintDocument margins. 

3. Intercept the print page event of the print document. 

4. Start the printing process by calling Print on the print document. 

5. In the print page event, print the chart and text together. 

Related Examples

Windows Forms: Printing\General 

Web Forms: This feature is not supported for web forms.

See Also

PrintManager | Print Manager Helper Methods | Build-In Print Preview