Click here to Skip to main content
12,945,575 members (51,849 online)
Click here to Skip to main content
Add your own
alternative version


167 bookmarked
Posted 8 Aug 2011

WPF Print Engine : Part I

, 8 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
WPF Print Engine makes is easier for .net developers working with WPF applications to leverage printing facility.
This is an old version of the currently published article.


Download - 938.96 KB


It was summer time. I was enjoying my time. Little did i know that my tough days were coming. Yes that’s right. I had been assigned the work on printing. I wouldn’t say it was very difficult. Its just that i had no idea how printing works in WPF world and to my surprise it wasn’t as easy as few Google searches. So after struggling a lot and spending some extra time apart from normal hours, i ended up with some pretty good experience that i want to share with you all. 


Printing is one of the bizarre part in all these years of my programming experience where i sometimes ran out clue and started figuring out solutions with guess work sometimes. It could just be that i was not good enough or could be that the thousands of different types of printers to handle were just not easy to tame. However it be, i landed up to a stage where i could address it quite fine to my requirements.

What is WPF Print Engine ?

WPF Print Engine makes is easier for .net developers working with WPF applications to leverage printing facility.

It is a standalone component that takes in few required and optional parameters to give encapsulate developers from all heavy lifting in order to deal with printing. 

In an attempt to explain things i split it into 5 sections as follows :

  • Demonstration : Overview of how it works
  • Usage : Code samples
  • DocumentPaginator : How the pagination is achieved
  • PrinterUtility : Retrieval of available printers and their properties / preferences  


WPF print engine comes with the following features at the moment :

  • Smart print preview to see what it would look like on after printing on the printer and paper you selected
  • Support for changing printer preferences directly
  • Scale page content in the print preview to fit in less pages
  • Turn on/off page numbers
  • Asynchronous printing directly to printer
  • Any WPF Visual Support
  • Any WPF FrameworkElement Support
  • DataTable Support 


The above snapshot shows the demo application.  As you can see it has as this stage, support for generating print preview for WPF Visual or a DataTable as input. Below is a snapshot of what we get then select “Print This Visual”. You can see that the visual is split in 2 separate pages because the current selected paper (A4) is smaller than the visual’s size.


You can change the printer, the printer preferences, the number of copies to print from the printing options tool. It also allows you to print the page numbers on each page or hide them.


There is one neat feature – “Print Size” that allows you to shrink the generated print preview content’s size to your will, so that you can fit it in less number of pages. This is done with the help of a slider giving you complete control over the resize ratio so that you can decide when you data is not being too small. This is unique in the sense that many applications allow you to shrink but not all allow you to control how much.


Notice below the updated snapshot after it has been shrunken just enough to fit into 1 pages instead of 2.


Similarly the demo application show an example for using DataTable as input for the print preview. Notice that the pagination is done such that no columns or row get cut and yet the maximum possible row or column is fit in the selected paper size.



Printing a WPF Visual: 

First you need to create an instance of the printcontrol which you can using the PrintControlFactory.Create method. It has few overloads, that i will be adding more to in future. One of those is the one that takes a size and a visual as input. Visual is the WPF visual (could be any control, panel, grid, window) anything that you want to print. When you specify a visual, all its children, as rendered on the screen are taken into consideration. 

var visualSize = new Size(visual.ActualWidth, visual.ActualHeight);
var printControl = PrintControlFactory.Create(visualSize, visual);

Printing a DataTable as source :

In order to give a datatable as a source, you will need to also supply the width of each column as a List<double>. Then instantiate a printcontrol using the PrintControlFactory.Create method that takes a datable and columnsWidths as arguments.

var columnWidths = new List<double>() {30, 40, 300, 300, 150};
var printControl = PrintControlFactory.Create(dataTable, columnWidths, headerTemplate);
Printing a DataTable as source with Header Template :

There is also an overload to give a header template. The header template to supply is a string of the Xaml File you will create. This can be in the form of a user control. To denote the page number and it place holder, simple place the verbatim string “@PageNumber” in its placeholder.


<UserControl x:Class="DEMOApplication.HeaderTemplate"





             mc:Ignorable="d" d:DesignWidth="919">
    <DockPanel LastChildFill="False" Margin="10">
        <Image Source="Images/headerDemo.gif" DockPanel.Dock="Left" Stretch="None" />
        <TextBlock DockPanel.Dock="Right" VerticalAlignment="Bottom" FontWeight="Bold" TextWrapping="Wrap" Text="Page Number : @PageNumber"/>
var columnWidths = new List<double>() {30, 40, 300, 300, 150};
var ht = new HeaderTemplate();
var headerTemplate = XamlWriter.Save(ht);
var printControl = PrintControlFactory.Create(dataTable, columnWidths, headerTemplate);


This is the just the introduction, to both the project and its documentation. At this moment i believe there are many areas that can be improved, including facility for the WPF DataGrid as input, support for footer template, refactoring the codebase more, better samples to name a few. I would love to welcome anyone interested in contributing to the source code and bring in better improvements and features.

Also i have plan to give Visual Studio design time support for creating printing templates. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Saraf Talukder
Software Developer
United Kingdom United Kingdom
Currently living and working in London. He is an enthusiastic software developer passionate about microsoft technologies, specially C#, WPF, Silverlight WCF and windows Azure. Contributes to several open source project and msdn forums.

My Blog
twitter : @sarafuddin

You may also be interested in...

Comments and Discussions

Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
Questionpublish this app Pin
f_3nik5-May-17 5:08
memberf_3nik5-May-17 5:08 
QuestionHeaderTemplate also for printing visuals? Pin
Member 1091956228-Feb-17 20:06
memberMember 1091956228-Feb-17 20:06 
QuestionAny idea how to find Part II? Pin
Civoneb19-Jan-17 1:03
memberCivoneb19-Jan-17 1:03 
BugCan't compile on W8.1 Pin
guton22-Nov-15 4:38
memberguton22-Nov-15 4:38 
GeneralRe: Can't compile on W8.1 Pin
guton22-Nov-15 5:07
memberguton22-Nov-15 5:07 
QuestionRegarding Printing on Page Pin
Suresh97247-Oct-15 1:02
memberSuresh97247-Oct-15 1:02 
QuestionI have a small project in WPF C# with RichTextBox I want to Print Pin
Member 1150699629-Sep-15 16:45
memberMember 1150699629-Sep-15 16:45 
BugMVVM implementation Pin
johnwrnd27-Mar-15 1:54
memberjohnwrnd27-Mar-15 1:54 
GeneralRe: MVVM implementation Pin
Saraf Talukder17-Jun-15 10:53
groupSaraf Talukder17-Jun-15 10:53 
QuestionHow to make the control print on full page Pin
Member 1107544630-Dec-14 18:18
memberMember 1107544630-Dec-14 18:18 
AnswerRe: How to make the control print on full page Pin
jogibear998815-Jul-15 21:17
memberjogibear998815-Jul-15 21:17 
QuestionFork @ github converted to .Net 4.0! Pin
jogibear99888-Apr-14 20:53
memberjogibear99888-Apr-14 20:53 
BugText overflow is not printed Pin
paparazzo4-Apr-14 7:15
memberpaparazzo4-Apr-14 7:15 
Questionvery helpful.....thanks Pin
Member 1047496718-Dec-13 6:02
memberMember 1047496718-Dec-13 6:02 
QuestionVisaul can be printed,but dataTable can not Pin
yuanstef15-Sep-13 1:09
memberyuanstef15-Sep-13 1:09 
AnswerRe: Visaul can be printed,but dataTable can not Pin
yuanstef15-Sep-13 17:12
memberyuanstef15-Sep-13 17:12 
QuestionExcellent work Pin
Chandan Choubey6-Aug-13 11:42
memberChandan Choubey6-Aug-13 11:42 
GeneralMy vote of 5 Pin
Shahin Khorshidnia27-Jul-13 5:40
memberShahin Khorshidnia27-Jul-13 5:40 
QuestionIs it possible to print selected pages from multiple pages? Pin
Vin Shen20-Jun-13 5:50
memberVin Shen20-Jun-13 5:50 
AnswerRe: Is it possible to print selected pages from multiple pages? Pin
Saraf Talukder20-Jun-13 6:02
groupSaraf Talukder20-Jun-13 6:02 
Questionblank print preview generate Pin
Ashhhhh Agarkar3-Feb-13 20:21
memberAshhhhh Agarkar3-Feb-13 20:21 
AnswerRe: blank print preview generate Pin
jogibear99885-Jul-13 3:39
memberjogibear99885-Jul-13 3:39 
QuestionComments Pin
Parhs28-Nov-12 17:00
memberParhs28-Nov-12 17:00 
AnswerRe: Comments Pin
Parhs1-Dec-12 4:44
memberParhs1-Dec-12 4:44 
QuestionHow to add header line text ? Pin
Ray Guan21-Nov-12 1:13
memberRay Guan21-Nov-12 1:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 8 Aug 2011
Article Copyright 2011 by Saraf Talukder
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid