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. 

Demo

Autodiagrammer by Sacha Barber now uses this print engine for its print preview facility  


推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"