Using FlowDocuments XAML to print XPS Documents. (Part 4)
Introduction
Welcome to Part 4 of the Using FlowDocument XAML to print XPS Documents series. This article will focus on saving XAML to an XPS Document file… finally.
In previous parts of this series I demonstrated creating DocumentPaginator, adjusting PageSize, and printing. Saving the XPS document is actually quite easy at this point. I will be building upon the classes discussed in Part 1, 2 and 3 of this series.
Creating a method to save an XPS document from a DocumentPaginator.
This method accepts a DocumentPaginator source and saves it to test.xps file, clean and simple. Obvious improvments would be to use a save file dialogue.
public static void CreateXPSDocument(DocumentPaginator paginator)
{
using (Package container = Package.Open("test" + ".xps", FileMode.Create))
{
using (XpsDocument xpsDoc = new XpsDocument(container, CompressionOption.Maximum))
{
XpsSerializationManager xpsSM = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
xpsSM.SaveAsXaml(paginator);
}
}
}
Using the CreateXPSDocument method.
private void SaveXPSButton_Click(object sender, RoutedEventArgs e)
{
SampleClass sample = new SampleClass();
sample.Sample = "FlowDocument DataBinding Sample";
IDocumentPaginatorSource flowDocument =
XamlTemplatePrinter.RenderFlowDocumentTemplate(Path.Combine(Environment.CurrentDirectory, "XamlDocumentTemplate.xaml"), sample);
flowDocument.DocumentPaginator.PageSize = new Size(96 * 8, 96 * 11);
XamlTemplatePrinter.CreateXPSDocument(flowDocument.DocumentPaginator);
}
Yes, it really was that easy. The XamlTemplatePrinter class we have created during this series is now actually becoming quite useful. This article will conclude this series for now, unless I think of something I might have forgotten.Please feel free to comment or leave suggestions for future articles.
Well… data binding doesn’t seem to work with the save code. Do you know how to make it work??
I cover databinding in xaml templates in another article. If you still have issues, please feel free to send me your code.
Do have any example in XSL template for transforming the XML(Data) into an XPS document format.
Thanks in Advance
Rehman,
Sorry I don’t, in these examples I am using XAML to contain the style and format of the template. I have not had a need to use XSL since I can use XAML.
Hello, great article, but there is problem with CreateXPS file, if i use databinding it simply doesnt work and data arent write to the XPS file. Im not sure whats wrong:-(
Cow-Killer,
Not sure what it would be that is causing this problem for you. I dont mind taking a look at your code if you want to post it or email it to me. Just let me know.
Hello,
thank you for your answer, i upload solution on rapidshare i hope it doesnt mind to you.
http://rapidshare.com/files/111672045/XPSPrinting.zip.html
Please take a look, im not sure whats wrong either, problem is with creating XPS file within aplication when i send it on virtual XPS printer it works as it should.
Thank you
I had a similar problem Cow-Killer, my immediate resolution was Part 5 of this series. I will take a longer look at the saving problem in your solution though.
Hello,
something new?
Hi RoeCode,
First of all thanks for your posts about this topic it was most helpfull to me.
I found a way to get the databinding to work when saving a FlowDocument to XPS. I created a post on my blog about it, you may find it interesting: http://blog.fohjin.com/2008/07/saving-wpf-xaml-flowdocument-to-xps.html
If in the meantime you found another/better way of doing this I am all ears
-Mark
Thank you – very usefull