Running on Empty

The few things I know, I like to share.

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.

Advertisements

January 4, 2008 - Posted by | C#, WPF, XAML, XPS

12 Comments »

  1. Well… data binding doesn’t seem to work with the save code. Do you know how to make it work??

    Comment by Mikael | February 13, 2008 | Reply

  2. I cover databinding in xaml templates in another article. If you still have issues, please feel free to send me your code.

    Comment by roecode | February 14, 2008 | Reply

  3. Do have any example in XSL template for transforming the XML(Data) into an XPS document format.

    Thanks in Advance

    Comment by Rehman Mansoor | February 26, 2008 | Reply

  4. 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.

    Comment by roecode | February 26, 2008 | Reply

  5. 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:-(

    Comment by Cow-Killer | April 30, 2008 | Reply

  6. 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.

    Comment by roecode | April 30, 2008 | Reply

  7. 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

    Comment by Cow-Killer | May 1, 2008 | Reply

  8. 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.

    Comment by roecode | May 1, 2008 | Reply

  9. Hello,

    something new?

    Comment by Cow-Killer | May 6, 2008 | Reply

  10. 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

    Comment by Mark Nijhof | July 12, 2008 | Reply

  11. Thank you – very usefull

    Comment by Søren Larsen | October 21, 2008 | Reply

  12. Thanks, this was very useful.

    Comment by Daniel | September 12, 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: