Search
Multimedia

Silverlight : Print a Document

Published 1 min readBy Programming4us Editorial Team
Silverlight 4 now contains printer support which you can easily take advantage of.

In Silverlight, to print, all you need to do is generate any UIElement-derived object and pass it to the printing system, as this code demonstrates:

private void buttonPrint_Click(object sender, RoutedEventArgs e)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += new EventHandler<PrintPageEventArgs>(doc_PrintPage);
doc.Print();
}

void doc_PrintPage(object sender, PrintPageEventArgs e)
{
//simply set the PageVisual property to the UIElement
//that you want to print
e.PageVisual = canvas;
e.HasMorePages = false;
}

The PrintPage event handler sets the PageVisual property to whatever control you want printed—it’s as easy as that.