Click here to Skip to main content
Click here to Skip to main content

WPF Print Engine : Part I

, 8 Aug 2011
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 WPFPrintEngine_Alpha_1.zip - 938.96 KB

Introduction

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. 

Background

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  

Demonstration

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 

 demoapp1.jpg
 

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.

visualPreview_thumb.png

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.

printingoption_thumb.png 

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.

printSize_thumb.png 

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

visualPreview1page_thumb.png 

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.

datatable_thumb.png

Usage

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);
printControl.ShowPrintPreview();  

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);
printControl.ShowPrintPreview(); 
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"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             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"/>
    </DockPanel>
</UserControl> 
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);
printControl.ShowPrintPreview(); 

Conclusion

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. 


License

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

Share

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
Follow on   Twitter

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.
 
QuestionFork @ github converted to .Net 4.0! Pinmemberjogibear99888-Apr-14 20:53 
BugText overflow is not printed Pinmemberpaparazzo4-Apr-14 7:15 
Questionvery helpful.....thanks PinmemberMember 1047496718-Dec-13 6:02 
QuestionVisaul can be printed,but dataTable can not Pinmemberyuanstef15-Sep-13 1:09 
AnswerRe: Visaul can be printed,but dataTable can not Pinmemberyuanstef15-Sep-13 17:12 
QuestionExcellent work [modified] PinmemberChandan Choubey6-Aug-13 11:42 
GeneralMy vote of 5 PinmemberShahin Khorshidnia27-Jul-13 5:40 
QuestionIs it possible to print selected pages from multiple pages? PinmemberVin Shen20-Jun-13 5:50 
AnswerRe: Is it possible to print selected pages from multiple pages? PingroupSaraf Talukder20-Jun-13 6:02 
Questionblank print preview generate PinmemberAshhhhh Agarkar3-Feb-13 20:21 
AnswerRe: blank print preview generate Pinmemberjogibear99885-Jul-13 3:39 
QuestionComments PinmemberParhs28-Nov-12 17:00 
AnswerRe: Comments PinmemberParhs1-Dec-12 4:44 
QuestionHow to add header line text ? PinmemberRay Guan21-Nov-12 1:13 
GeneralMy vote of 5 PinmemberRay Guan21-Nov-12 0:50 
Questionpreview is ok but nothing is printed Pinmemberlucky_log200016-Oct-12 23:03 
AnswerRe: preview is ok but nothing is printed PingroupSaraf Talukder17-Oct-12 6:45 
GeneralRe: preview is ok but nothing is printed Pinmemberlucky_log200018-Oct-12 4:33 
GeneralBrilliant! Pinmemberwoutercx5-Sep-12 6:05 
GeneralRe: Brilliant! PingroupSaraf Talukder17-Oct-12 6:59 
QuestionDeployment [modified] PinmemberAaron Edwards15-Aug-12 7:47 
AnswerRe: Deployment Pinmemberjagdish bidwai29-Jan-13 0:43 
GeneralMy vote of 1 Pinmemberravitej0079-Jun-12 20:54 
GeneralRe: My vote of 1 PingroupSaraf Talukder17-Oct-12 6:57 
GeneralRe: My vote of 1 PinmemberShahin Khorshidnia27-Jul-13 5:42 

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

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

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