One of Silverlight’s most requested features – Print makes its debut with Silverlight 4. Silverlight’s Print functionality allows you to specify a XAML tree to print. The primary class for printing is the PrintDocument class which exposes several events which are used to call back ask how to print individual pages:
PrintDocument printDoc = new PrintDocument(); printDoc.DocumentName = "SilverLight 4 Demo Print"; printDoc.StartPrint += new EventHandler<StartPrintEventArgs>(printDoc_StartPrint); printDoc.PrintPage += new EventHandler<PrintPageEventArgs>(printDoc_PrintPage); printDoc.Print();
The PrintDocument class allows the wire of up pre- and post-print events to prepare and clean-up code. The important element is PrintPage where the UIElement to print is passed to the PrintDocument object. This UIElement be something in the visual tree or something created virtually in-memory and not added to the visual tree.