WPF Print Engine : Part I
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
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.
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.
Demo
Autodiagrammer by Sacha Barber now uses this print engine for its print preview facility
Post Comment
As a Newbie, I am continuously exploring online for articles that can be of assistance to me.
the problem now is with footer !!!
Regards for helping out, excellent information.
hZ1640 outlet went on to play the Angels in this ministry will
Thanks for the article post.Really looking forward to read more. Awesome.
This is a exceptionally hairy post. I like the way you used your words.
Below youll discover the link to some websites that we think it is best to visit.
Thank you for your blog post.Thanks Again. Much obliged.
CtSSmJ I will right away grab your rss as I can not find your e-mail subscription link or e-newsletter service. Do you have any? Kindly let me know in order that I could subscribe. Thanks.
fKIOjS Muchos Gracias for your post.Really thank you! Really Cool.
6UU9Zl This unique blog is really cool as well as informative. I have chosen a lot of helpful things out of this amazing blog. I ad love to go back every once in a while. Thanks!
RULujm Thanks-a-mundo for the blog post.Really looking forward to read more. Great. sex photos
vmULTv This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
hfkGu7 I truly appreciate this article post. Keep writing.
6h37Bg Thanks for sharing, this is a fantastic article post. Really Cool.
uJ3mgF Perfect piece of work you have done, this website is really cool with great info.
VdbAWi Thanks a lot for the article.Really looking forward to read more. Fantastic.
WlVzmx Muchos Gracias for your article post. Will read on
OBQjCS Very nice info and straight to the point. I don at know if this is actually the best place to ask but do you people have any ideea where to employ some professional writers? Thank you
gBSQaT Very interesting info !Perfect just what I was looking for! The right to be heard does not autmatically include the right to be taken seriously. by Hubert Humphrey.
IeASCR You have noted very interesting points! ps nice website.
Really appreciate you sharing this blog article.Much thanks again. Want more.
2nd8K8 Thanks so much for the blog article.Really looking forward to read more. Will read on
O0ITeU I value the blog post.Really thank you! Much obliged.
Gnb5gS
mO1Je6 I think you have observed some very interesting points, regards for the post.
UQdprT Wow, great post.Much thanks again. Cool.
R0AZ66 the most beneficial in its field. Awesome blog!
NQ04By A big thank you for your blog article.Thanks Again. Will read on...
G7iLJO Thanks a lot for the blog article.Much thanks again. Will read on
eH4ueh As I site possessor I believe the content matter here is rattling fantastic , appreciate it for your hard work. You should keep it up forever! Good Luck.
ZVr6NU You made some good points there. I did a search on the issue and found most individuals will consent with your website.
Eqrupy I have recently started a site, the information you offer on this site has helped me tremendously. Thank you for all of your time & work.
9MZcLr Muchos Gracias for your post. Fantastic.
TiBwiu Valuable info. Fortunate me I discovered your site accidentally, and I'm stunned why this accident didn't took place earlier! I bookmarked it.
BlFAF5 Muchos Gracias for your blog article.Much thanks again. Keep writing.
z1vI8F Say, you got a nice article post.Really thank you! Want more.
p8o4Zt Great, thanks for sharing this post.Thanks Again. Fantastic.
bU8mQr Thanks-a-mundo for the blog post.Much thanks again. Awesome.
pOrGYV Very informative blog post.Really thank you! Really Great.
uNhBsF Thank you for your article post.Really thank you!
fAdnSD Thanks again for the post.Really looking forward to read more. Really Great.
HNU8O0 Thanks so much for the blog. Will read on...
oonGEB Thanks a lot for the blog article.Much thanks again. Really Great.
mKWpnd wow, awesome article post. Will read on...
faNGVE Thanks a lot for the article.Thanks Again.
KorBrP I really liked your article. Really Great.
sa4rLD Im thankful for the post.Much thanks again. Fantastic.
TN7zpH A round of applause for your article.Really looking forward to read more. Great.
AuQJ8w Thanks so much for the article.Really looking forward to read more. Cool.