Running on Empty

The few things I know, I like to share.

Using FlowDocuments XAML to print XPS Documents. (Part 5)

Introduction

Welcome to Part 5 of the Using FlowDocument XAML to print XPS Documents series.  This article has been a long time coming.  In this article I will focus on creating dynamic XAML content.

Creating a method to Load Xaml Strings

This method simply takes a raw XAML string and loads it into a FlowDocument.

        public static IDocumentPaginatorSource RenderFlowDocumentString(string rawXamlString, object dataContextObject)
        {
            FlowDocument document = XamlReader.Load(new XmlTextReader(new StringReader(rawXamlString))) as FlowDocument;
            if (dataContextObject != null)
            {
                document.DataContext = dataContextObject;
            }
            return document;
        }

Now all we need to do is create some XAML strings.

Using the new Xaml string loader plus some.

        public void PrintGenericList(List<Awards> awardList)
        {
            PrintDialog flowPrintDialog = XamlTemplatePrinter.GetPrintDialog();
            if (flowPrintDialog == null)
                return;


                StringBuilder xamlString = new StringBuilder();
                
                xamlString.Append("<FlowDocument xmlns=\"<a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation\">http://schemas.microsoft.com/winfx/2006/xaml/presentation\</a>" \nxmlns:x=\"<a href="http://schemas.microsoft.com/winfx/2006/xaml\">http://schemas.microsoft.com/winfx/2006/xaml\</a>">");
                
                foreach (Awards award in awardList)
                {
                    xamlString.Append("<Paragraph FontFamily=\"Arial\" Margin=\"20\">");
                    xamlString.AppendFormat("<TextBlock Text=\"{0}  {1}\" Margin=\"170,0,0,0\" />", award.SerialNumber, award.ControlNumber);
                    xamlString.Append("<TextBlock Text=\"\" Margin=\"170,0,0,0\" />");
                    xamlString.Append("</Paragraph>");
                }

                xamlString.Append("</FlowDocument>");

                IDocumentPaginatorSource flowDocument =
                       XamlTemplatePrinter.RenderFlowDocumentString(xamlString.ToString(), null);

                flowDocument.DocumentPaginator.PageSize = new Size(96 * 5, 96 * 2);

                PrintQueue flowPrintQueue = flowPrintDialog.PrintQueue;
                XamlTemplatePrinter.PrintFlowDocument(flowPrintQueue, flowDocument.DocumentPaginator);            
        }

Conclusion

In this article I demonstrated building dynamic XAML to print or same as an XPS document.  I am personally using this in an application to format labels to be sent to a printer.

About these ads

April 30, 2008 - Posted by | C#, WPF, XAML, XPS

4 Comments »

  1. Another great article. Is there any reason why this is the only way I have found to create a dynamic flow document with databinding that I can pass to a DocumentViewer?

    Thanks,
    Linda

    Comment by Linda Rawson | May 13, 2008 | Reply

  2. No there isn’t any real reason other than there are not many people experimenting with flow documents currently. This series is my attempt to fill a knowledge gap that I had a year and a half ago when I first started WPF. I am certain there is more to learn and as soon as my personal understanding has grown I will share on this blog.

    Comment by roecode | May 14, 2008 | Reply

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

  4. Thanks great article again.

    Comment by shahab | August 28, 2008 | 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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: