Windows Presentation Foundation (WPF) offers a wide range of document features that enable the creation of high-fidelity content that is designed to be more easily accessed and read than in previous generations of Windows. In addition to enhanced capabilities and quality, WPF also provides integrated services for document display, packaging, and security.

Sounds impressive, let’s put it to the test to see if we can get some of the “Desktop-Publishing” features that we need to satisfy our requirements.

·By now, you should already be familiar with the concept that a WPF Control can be housed inside Dynamics (and interact seamlessly with it). This article will cover the creation of a WPF UserControl that will display data merged documents inside Dynamics.

An SSRS generated report is nothing more than an A4 sized “windows-form” that contains labels, data and graphics (or whatever your paper-size). This is no different to the concept of generating a form within WPF.

WPF allows the developer to create the page by positioning data and graphic elements onto a form surface (called a “canvas”). This is all done via an Xml markup language called XAML. There are a few “visual-designers” in the marketplace that allow you to “mock-up” your canvas; however, nothing currently surpasses the native “Metro-style” designer available inside Visual Studio 2012. Unfortunately, this version of Visual Studio is not currently compatible with Dynamics 2012 (despite them both having “2012” in their titles!)

I’m going to concentrate on “FixedDocuments” as this will address all the issues raised thus far. “FlowDocuments” will only give us the same dynamic resizing problems that we face in SSRS. Fixed documents can be rendered and written directly to disk, but during the design phase you need to see what you are working on so a Windows “DocumentViewer” control is required. This control is defined and initialised as follows:

Xaml

<DocumentViewer Name="_viewer" Width="Auto" Height="Auto"/>

Code

// create a document (collection of pages) and set the default page dimensions to match the printer

// create page and set the default page dimensions to match the printer

FixedPage page = newFixedPage();

page.Width = document.DocumentPaginator.PageSize.Width;

page.Height = document.DocumentPaginator.PageSize.Height;

...code to populate document

// preview the document

_viewer.Document = document;

·As it stands nothing will be displayed yet in the DocumentViewer control.

The next thing to discuss is duplex printing. We need to be able to print our report so that there is a “reverse-page” printing option for things like “terms and conditions”. This is done via something known as “Page-Sequencing” of “Page-Masters”.

Let’s take a very simple example of an Invoice that requires some terms and conditions on the back. First you need to define two page-masters. This is done by adding two “user-controls” to your Wpf project that will house the front-page and the reverse-page:

If you compile and run your project, then you will be able to see 2 pages within the DocumentViewer control. You can zoom-in, resize and arrange the pages as you would in any standard document viewer. Try and print this document using the printer icon button:

·If you have a duplex enabled printer then you will get double-sided printing.

This is not very glamourous (or useful) yet, but it has demonstrated the fundamental principle of page-sequencing. We now have the ability to decide which “page-master” will print and in what order. In the next article I will be discussing how we can go about populating these pages with some meaningful data and how to handle page-overflow.