New to Telerik UI for WinForms? Download free 30-day trial

Create Printable Panel

This example shows how you can implement printable panel. This can be very useful when you want to print several controls at once. To achieve this you can create a custom panel that inherits from RadPanel and implements the IPrintable interface. This interface contains four methods:

  • BeginPrint: Called when the printing begins.

  • EndPrint: Called when the printing ends.

  • PrintPage: Prints the page with the specified number.

  • GetSettingsDialog: Gets a print settings dialog that is specific for the printable object.

The first step would be to just create the custom class:


public class PrintablePanel : RadPanel, IPrintable

Public Class PrintablePanel
    Inherits RadPanel
    Implements IPrintable

For the current example the BeginPrint and EndPrint methods should just return a hard-coded values:


public int BeginPrint(RadPrintDocument sender, PrintEventArgs args)
{
    return 1;
}

public bool EndPrint(RadPrintDocument sender, PrintEventArgs args)
{
    return true;
}

Public Function BeginPrint(sender As RadPrintDocument, args As PrintEventArgs) As Integer Implements IPrintable.BeginPrint
    Return 1
End Function
Public Function EndPrint(sender As RadPrintDocument, args As PrintEventArgs) As Boolean Implements IPrintable.EndPrint
    Return True
End Function

In the GetSettingsDialog method you should just return an new settings dialog:

public Form GetSettingsDialog(RadPrintDocument document)
{
    return new PrintSettingsDialog(document);
}

Public Function GetSettingsDialog(document As RadPrintDocument) As Form Implements IPrintable.GetSettingsDialog
    Return New PrintSettingsDialog(document)
End Function

The PrintPage method is the place where the panel would be converted to image and drawn:


public bool PrintPage(int pageNumber, RadPrintDocument sender, PrintPageEventArgs args)
{
    Bitmap bmp = new Bitmap(this.Width, this.Height);
    this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size));
    args.Graphics.DrawImage(bmp, Point.Empty);

    return false;
}

Public Function PrintPage(pageNumber As Integer, sender As RadPrintDocument, args As PrintPageEventArgs) As Boolean Implements IPrintable.PrintPage
    Dim bmp As New Bitmap(Me.Width, Me.Height)
    Me.DrawToBitmap(bmp, New Rectangle(Point.Empty, Me.Size))
    args.Graphics.DrawImage(bmp, Point.Empty)
    Return False
End Function

Besides the interface methods implementation you can create the Print and PrintPreview methods:


public void Print()
{
    RadPrintDocument doc = this.CreatePrintDocument();
    doc.Print();
}

public void PrintPreview()
{
    RadPrintDocument doc = this.CreatePrintDocument();
    RadPrintPreviewDialog dialog = new RadPrintPreviewDialog(doc);

    dialog.ThemeName = this.ThemeName;
    dialog.ShowDialog();
}

Public Sub Print()
    Dim doc As RadPrintDocument = Me.CreatePrintDocument()
    doc.Print()
End Sub
Public Sub PrintPreview()
    Dim doc As RadPrintDocument = Me.CreatePrintDocument()
    Dim dialog As New RadPrintPreviewDialog(doc)
    dialog.ThemeName = Me.ThemeName
    dialog.ShowDialog()
End Sub

The final method is CreatePrintDocument. It returns a new RadPrintDocument associated with the current instance:

private RadPrintDocument CreatePrintDocument()
{
    RadPrintDocument doc = new RadPrintDocument();
    doc.AssociatedObject = this;
    return doc;
}

Private Function CreatePrintDocument() As RadPrintDocument
    Dim doc As New RadPrintDocument()
    doc.AssociatedObject = Me
    Return doc
End Function

See Also

In this article