Tag: Printing

WPF Printing – some issues resolved

After my recent post I emailed the development team at Microsoft and some issues have been worked out.

When printing a landscape document via the preview control you need to set a print ticket on the FixedDocument before displaying it in preview.

FixedDocument x = new FixedDocument();
PrintTicket pt = new PrintTicket();
pt.PageOrientation = PageOrientation.Landscape;
x.PrintTicket = pt;

You can then set the Document property of the DocumentViewer and when you print from there it should print landscape.

I haven’t tested this in anger because I moved on and found a neater approach (and I didn’t really want a preview of what I was printing I simply wanted to avoid wasting paper and ink).

I’ve since set up a PDF print queue using RedMon (great wee utility) and GhostScript so I’ve been using that to produce PDFs which I can look at.

A much tidier approach to printing involves printing objects that derive from the Visual class (and all FrameworkElement objects in  WPF do). So I created a new Page (although you could also base a new class on UserControl) and called it EntrySheet, set up the content as I wish and then to print I do the following:

PrintDialog pd = new PrintDialog();
pd.PrintTicket.PageOrientation = PageOrientation.Landscape;

Size ps = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);

EntrySheet p = new EntrySheet();
p.Width = ps.Width;
p.Height = ps.Height;

p.Measure(ps);
p.Arrange(new Rect(new Point(0, 0), ps));
p.UpdateLayout();

pd.PrintVisual(p, "Entry Sheet");

Because the EntrySheet object is not being displayed prior to printing, you must call Measure and Arrange so that it’s content can be dimensioned and layed out. UpdateLayout isn’t always needed but I found that if your Visual includes a DataGrid then this step is essential.

The issue I haven’t been able to resolve is a DataGrid where the columns have been given star dimensions.

Tags: ,

Trials and Tribulations with WPF Printing

I’m developing a program to do sailing race results for my club. After some deliberation I elected to use WPF 4 as it finally includes the DataGrid control which I’ve always thought was essential. I’ll blog about things I’ve done to make editing and entering results as easy as possible soon as that was an interesting learning curve and what at first appeared to be quite limited is finally working well.

With regards to printing WPF provides some very interesting and very powerful features and overall I’m impressed at how easy it is to produce printed output, though there are a few oddities.

These mostly came to light as I started working on printing race entry sheets which are set out before the race for competitors to enter with a set of boxes for the Race Officer to enter lap finish times. Essentially what I wanted was a single page containing a grid with equal size columns laid out landscape.

First I set up a Page with a DataGrid and fixed columns giving each column a star width. I added 15 empty rows to the grid to get a table. Getting a Page into a FixedDocument (which I will also detail elsewhere), is a bit of a palaver but I’d already worked out how to do that and so I got that done and that for Landscape I needed to change the PageOrientation on FixedDocument’s DocumentPaginator.

First problem, did a preview using a DocumentViewer and things look good, but print and it comes out Portrait mode on the paper and half my grid is missing, and after much searching I found the following:

http://stackoverflow.com/questions/1003585/setting-pageorientation-for-the-wpf-documentviewer-printdialog

I’m already using a custom template for a DocumentViewer to remove the Search box, and the template I’d got from the standard control doesn’t have a print button and so there is no way to intercept the print command that I could see.

Two issues with DocumentViewer in my opinion:

1. Need to be able to turn off the search facility
2. Need simple way to specify print orientation or it should work it out for itself (I’m passing a FixedDocument after all which knows the orientation!).

So I looked at printing without previewing.

The next issue which caused much head scratching, the DataGrid columns weren’t sized equally when printed but instead were given a minimal size despite the grid stretching across the page. I tried calling Measure and Arrange on the Page and on the DataGrid but neither helped. Eventually I discovered I had to call BeginInit and EndInit after Measure and Arrange to do the sizing of the columns.

The dynamic DataGrid column sizing didn’t happen because I wasn’t previewing, i.e. making the document visible.

What I evenually also realised was that I didn’t need the Page at all or the FixedDocument, I could create a UserControl and set it’s size to the page, Measure, Arrange, BeginInit and EndInit and then print directly as a Visual.

Tags: , ,

Trojan Archer