WPF Printing Part 1 – Printing Visuals

This is the first part of a series of posts about printing in WPF starting from printing a single element and going all the way to advanced topics like background printing and XPS.

We will start this series with absolutely the simplest WPF application that prints something, we will create a window with a single button – and we will then print this button.

The gateway into the WPF printing system is the System.Windows.Controls.PrintDialog class, this class manages printer settings and let you do the actual printing, it is also, as the name suggests, the print dialog – but you don’t have to show the print dialog in order to use it to print.

PrintDialog has two methods you can use to print stuff, PrintVisual and PrintDocument, PrintVisual is the easier of the two so we’ll start with it – it’s also mostly useless in real world scenarios so we probably wouldn’t talk about it again in this series.

Let’s start, create a new WPF application and add this inside Window1.xaml:

<Button Name=”PrintButton” Content="Print Me!" Click="Print_Click"/>

And this in the Window1.xaml.cs file:

private void Print_Click(object sender, RoutedEventArgs e)
{
    PrintDialog dlg = new PrintDialog();
    dlg.PrintVisual(PrintButton, "Print Button");
}

Those two lines of code will print an image of the button to the default printer, the “Print Button” string is the document name you can see it if you open the printer queue window (Start -> Control Panel -> Printers and Faxes -> printer name).

Here is a slightly longer version that will let the user choose a printer:

private void Print_Click(object sender, RoutedEventArgs e)
{
    PrintDialog dlg = new PrintDialog();
    if (dlg.ShowDialog() == true)
    {
        dlg.PrintVisual(Print, "Print Button");
    }
}

In the next post of this series we will talk about how to choose a printer and change printer settings programmatically.

posted @ Thursday, March 19, 2009 10:45 AM

Comments on this entry:

# re: WPF Printing Part 1 – Printing Visuals

Left by Carlos at 7/20/2010 7:49 PM

Great, thanks. I have a question, how you can print a visual that is not visible in the screen? For instance: You have a TabPanel and you want to print a button that is on a non selected tab. Thanks...

# re: WPF Printing Part 1 – Printing Visuals

Left by Nir at 7/21/2010 12:30 AM

Hi Carlos,

I think PrintVisual should work with visuals that aren't on-screen but I've never tested it.

I believe print visual is never capable enough for real world usage and you should use a FixedDocument for printing, if you follow the link labeled “next post” at the end of the post you will see how to create a FixedDocument.

In the example I’ve added a TextBlock to the document, to print a visual just add a Rectangle and set it’s Fill property to a VisualBrush, then set the brush’s Visual property to the visual you want to print.

I have used VisualBrush with off-screen visuals in the past and it works perfectly, just remember to set the brush’s Stretch property and the rectnagle width and height or the sizing will be wrong.

# re: WPF Printing Part 1 – Printing Visuals

Left by Joel at 11/5/2011 12:50 AM

i used your fixeddocument code and it print blank??

Your comment:



 (will not be displayed)


 
 
Please add 6 and 3 and type the answer here: