Chart for .NET / User's Guide / Printing / Custom Printing

In This Topic
    Custom Printing
    In This Topic

    The chart document has a special kind of view called NChartPrintView allowing you to print the current chart contents on a printer device at the specified position (rectangle). The following example prints a custom header and footer and the chart contents on the page:

    C#
    Copy Code
    ...
     PrintDocument printDocument = new PrintDocument();
     printDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
    ... 
    
    private void OnPrintPage(object sender, PrintPageEventArgs ev)
    {
        RectangleF marginBounds = ev.MarginBounds;
    
        // create header and footer
        string header = "Custom Header";
        string footer = "Custom Footer";
    
        Font font = new Font("Arial", 15);
        Brush brush = new SolidBrush(Color.Black);
    
        // measure them
        SizeF headerSize = ev.Graphics.MeasureString(header, font);
        SizeF footerSize = ev.Graphics.MeasureString(footer, font);
    
        // draw header
        RectangleF headerBounds = new RectangleF(marginBounds.Left, marginBounds.Top, marginBounds.Width, headerSize.Height);
        ev.Graphics.DrawString(header, font, brush, headerBounds);
    
        // draw chart
        NRectangleF chartBounds = new NRectangleF(marginBounds.Left, headerBounds.Bottom, marginBounds.Width, marginBounds.Height - headerSize.Height - footerSize.Height);
    
        using (NChartPrintView printView = new NChartPrintView(chartControl.Document, ev.Graphics))
        {
      printView.Print(chartBounds);
        }
    
        // draw footer
        RectangleF footerBounds = new RectangleF(marginBounds.Left, marginBounds.Bottom - footerSize.Height, marginBounds.Width, footerSize.Height);
        ev.Graphics.DrawString(footer, font, brush, footerBounds);
    
        // dispose objects
        font.Dispose();
        brush.Dispose();
    }
    
    Visual Basic
    Copy Code
    ....
    Dim printDocument As New PrintDocument
    AddHandler printDocument.PrintPage, AddressOf OnPrintPage
    ...
    
    Private Sub OnPrintPage(ByVal sender As System.Object, ByVal ev As PrintPageEventArgs)
        Dim marginBounds As Rectangle = ev.MarginBounds
    
        ' create header and footer
        Dim header As String = "Custom Header"
        Dim footer As String = "Custom Footer"
    
        Dim font As New Font("Arial", 15)
        Dim brush As New SolidBrush(Color.Black)
    
        ' measure them
        Dim headerSize As SizeF = ev.Graphics.MeasureString(header, font)
        Dim footerSize As SizeF = ev.Graphics.MeasureString(footer, font)
    
        ' draw header
        Dim headerBounds As RectangleF = New RectangleF(marginBounds.Left, marginBounds.Top, marginBounds.Width, headerSize.Height)
        ev.Graphics.DrawString(header, Font, Brush, headerBounds)
    
        ' draw chart
        Dim chartBounds As RectangleF = New RectangleF(marginBounds.Left, headerBounds.Bottom, marginBounds.Width, marginBounds.Height - headerSize.Height - footerSize.Height)
        Dim printView As NChartPrintView = New NChartPrintView(m_ChartControl.Document, ev.Graphics)
        Try
           printView.Print(chartBounds)
        Finally
           CType(printView, IDisposable).Dispose()
        End Try
    
        ' draw footer
        Dim footerBounds = New RectangleF(marginBounds.Left, marginBounds.Bottom - footerSize.Height, marginBounds.Width, footerSize.Height)
        ev.Graphics.DrawString(footer, Font, Brush, footerBounds)
    
        ' dispose objects
        Font.Dispose()
        Brush.Dispose()
    End Sub
    
     Related Examples
    Windows Forms: Printing\Custom Printing
    See Also