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
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;
}
In the GetSettingsDialog method you should just return an new settings dialog:
public Form GetSettingsDialog(RadPrintDocument document)
{
return new PrintSettingsDialog(document);
}
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;
}
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();
}
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;
}