ARTICLE

Printing in WPF using VB.NET

Posted by Mahesh Chand Articles | WPF using VB.NET June 12, 2010
This code snippet shows how to print a control, user control or a Window in WPF using VB.NET.
 
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.   

Dim printDlg As New PrintDialog
Dim uc As New Button()
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.

Dim printDlg As New PrintDialog
printDlg.PrintVisual(Grid1, "Grid Control 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.

Dim printDlg As New PrintDialog
printDlg.PrintVisual(Me, "Window Printing.")

 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor