ARTICLE

Printing a Control in WPF using C#

Posted by Mahesh Chand Articles | WPF June 12, 2010
This code snippet explains how to print a control, user control, container, or a Window in WPF using C#.
Reader Level:

Print a Control, User Control, or Window in WPF

In WPF, a Visual is an object that is parent class of all user interfaces including UIElement, Containers, Controls, UserControls, and even Viewport3DVisual. If you notice all control or user controls classes, they are inherited from a UIElement class.

The PrintVisual print a Visual object. That means, by using the PrintVisual method, we can print any control, container, Window or user control.

The following code snippet in creates a PrintDialog object and calls its PrintVisual method by passing a UserControl to print the UserControl. Using this method, we can print any controls in WPF including a Window, page, or a ListBox.   

PrintDialog printDlg = new PrintDialog();
UserControl1 uc = new UserControl1();
printDlg.PrintVisual(uc, "User Control Printing."); 

 

What if you want to print a Grid control or any other control?

As said above, printing any control in WPF is same process. Just pass the Grid or other control in the PrintVisual method of PrintDialog.

PrintDialog printDlg = new PrintDialog();

printDlg.PrintVisual(grid1, "Grid Printing.");

How about printing the entire Window?

For entire window, you can either pass the window object or this keyword. This time, you pass "this" that is the object of current Window where you are writing this code.

PrintDialog printDlg = new PrintDialog();

printDlg.PrintVisual(this, "Window Printing.");

 

Login to add your contents and source code to this article
post comment
     

How can print multiple records of a listview. PrintVisual only provide image of a specific size or view (where you stand at a time).Please help to print multiple records with the help of printvisual or any other method.i'm new in wpf please explain in brief if any one know(My listview binds with other user control and repeat this use control according to data size)

Posted by Jack May 01, 2012

how can I print the window in landscape without opening the print dialog?

Posted by ernst kugler Jul 13, 2011

As of VS 2008 Microsoft appears to have dropped the PrintVisual method from the PrintDialog class. Another example of a reason not to upgrade all of the time.

Posted by Tom May 24, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter