views:

7000

answers:

6

Can I take an Existing WPF (XAML) Control, databind it and turn it into an XPS document that can be displayed and printed using the WPF XPS Document Viewer? If so, how? If not, how should I be doing ‘reporting’ in WPF using XPS/PDF/etc?

Basically I want to take an existing WPF control, databind it to get useful data into it and then make it printable and saveable for the end user. Ideally the document creation would be done in memory and wouldn’t hit the disk unless the user specifically saved the document. Is this feasible?

+6  A: 

Actually after messing around with heaps of different samples, all of which are incredibly convoluted and require the use of Document Writers, Containers, Print Queues and Print Tickets, I found Eric Sinks article about Printing in WPF
The simplified code is a mere 10 lines long

public void CreateMyWPFControlReport(MyWPFControlDataSource usefulData)
{
  //Set up the WPF Control to be printed
  MyWPFControl controlToPrint;
  controlToPrint = new MyWPFControl();
  controlToPrint.DataContext = usefulData;

  FixedDocument fixedDoc = new FixedDocument();
  PageContent pageContent = new PageContent();
  FixedPage fixedPage = new FixedPage();

  //Create first page of document
  fixedPage.Children.Add(controlToPrint);
  ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
  fixedDoc.Pages.Add(pageContent);
  //Create any other required pages here

  //View the document
  documentViewer1.Document = fixedDoc;
}

My sample is fairly simplistic, it doesn't include Page Sizing and Orientation which contains a whole different set of issues that don't work as you would expect. Nor does it contain any save functionality as MS seem to have forgotten to include a Save button with the Document Viewer.

Save Functionality is relatively simple (and is also from Eric Sinks article)

public void SaveCurrentDocument()
{
 // Configure save file dialog box
 Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
 dlg.FileName = "MyReport"; // Default file name
 dlg.DefaultExt = ".xps"; // Default file extension
 dlg.Filter = "XPS Documents (.xps)|*.xps"; // Filter files by extension

 // Show save file dialog box
 Nullable<bool> result = dlg.ShowDialog();

 // Process save file dialog box results
 if (result == true)
 {
   // Save document
   string filename = dlg.FileName;

  FixedDocument doc = (FixedDocument)documentViewer1.Document;
  XpsDocument xpsd = new XpsDocument(filename, FileAccess.ReadWrite);
  System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
  xw.Write(doc);
  xpsd.Close();
 }
}

So the answer is Yes, you can take an Existing WPF (XAML) Control, databind it and turn it into an XPS document - and its not all that difficult.

Scott
Could you please provide the definitions for MyWPFControl and MyWPFControlDataSource ??? The example code is worthless without them and the Sinks article does not seem to contain them.
Jim Beam
Jim, MyWPFControl is any control (custom, composite, standalone or otherwise) that you want to have rendered as a page within your XPS document. MyWPFControlDataSource is obviously any data, (usually a ViewModel), you want to bind to that control. The example has deliberately been left in a generic form so that its useful for anyone looking at it, not just someone looking to render a specific control to xaml.
Scott
A: 

Hello Scott,

I am wondering how to implement this technique for multiple tabpages in a tabcontrol in WPF?

Thx. KJ

KJ
KJ - if you want to post an official question I'll have a go at answering - we don't really get enough space in a comment to give a full blown answer.I imagine you could treat each tabpage in a way similar to the controlToPrint variable in the answer here.
Scott
A: 

Have any of you worried that the file size of the XPS document in this manner his much larger. I am seeing 100k for a simple textual document that was built off of controls.

Not really :)We do some pretty intense graphical work and our file sizes are generally below 300k which is fine for us. Be careful of raster images in your xaml as these tend to blow out the xps size dramatically. Rename your .xps to a .zip and have a look inside to get a better idea of where your file size is blowing out.
Scott
A: 

And how can I divide long control on many pages? I'd like to print very long list, but i'd like to have my custom header on each page...

Simon
Simon, It's been a long time since I had to look at this stuff so I don't know the answer off the top of my head. You should post a separate official question and reference this question, I'm sure someone who's more up-to-date on this stuff than I am will have no problem answering.
Scott
Thx for answer I've already found out how to do that :) DocumentPaginator is great way to deal with such problems.
Simon