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.
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
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.
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
Thanks great article again.