WPF Documents

By | February 25, 2010

Windows Presentation Foundation (WPF) provides rich set of features that help work with print ready and authoring documents easily.

 

WPF supports two types of Document models which provide rich layout support for displaying large amounts of text combined with features like scrolling, pagination and zoom.

 

Types of Documents:

  • Fixed Documents are XPS (Open XML Paper Specification) based fixed type set documents which are print ready
  • Flow Documents are dynamic which can layout the content dynamically based on details such as size of window and resolution.

Fixed document Flow document

       

WPF provides different kinds of Document containers to display these documents.

  • DocumentViewer allows to show fixed documents (XPS documents) in a window. (readonly) –Shown above
  • FlowDocumentPageViewer, FlowDocumentReader and FlowDocumentScrollViewer containers are read only which displays flow documents. (XAML documents) – Shown Above

FlowDocumentScrollViewer:

FlowDocumentScrollViewer shows the entire document with a scroll bar to let you move through it if the document exceeds the size of the FlowDocumentScrollViewer. The FlowDocumentScrollViewer doesn’t support pagination or multicolumn displays.

  • Printing and Zooming is supported.
  • Toolbar is hidden by default. To make it visible, set IsToolbarVisible to true
  • To disallow text selection in flowDocumentScrollViewer, use FlowDocumentScrollViewer.IsSelectionEnabled to false.

image

 

FlowDocumentPageViewer:

FlowDocumentPageViewer splits a flow document into multiple pages. Each page is as large as the available space and the user can step from one page to the next. The Flow-DocumentPageViewer has more overhead than the FlowDocumentScrollViewer  due to pagination feature.

  • Printing and Zooming is supported.
  • Toolbar is enabled by default.

image

 

FlowDocumentReader:

FlowDocumentReader combines the features of the FlowDocumentScrollViewer and FlowDocumentPageViewer. It lets the user choose whether to read content in a scrollable or paginated display. It also includes searching functionality.

The FlowDocumentReader has the most overhead of any flow document container. 

 

It supports three modes to display document.

  1. ScrollBarMode
  2. TwoPageMode
  3. PageMode
  • Printing and Zooming is supported.
  • Toolbar is enabled by default.
  • Document search functionality is supported.

image

 

Preserving space in WPF documents:

 

You need to use the xml:space attribute with the value preserve, which is an XML convention that tells an XML parser to keep all the whitespace characters in the content

 

 

Fixed Document snippets:

 

Code Snippet
  1. using System.Windows;
  2. using System.Windows.Documents;
  3. using System.Windows.Markup;
  4. using System.Windows.Xps.Packaging;
  5. using System.Xml;

 

 

Creating a simple Fixed document Programmatically:

 

Code Snippet
  1. var fixedDocument = new FixedDocument();
  2.             PageContent pageContent = new PageContent();
  3.             fixedDocument.Pages.Add(pageContent);
  4.             FixedPage fixedPage = new FixedPage();
  5.             TextBlock textBlock = new TextBlock
  6.                                       {
  7.                                           Margin = new Thickness(10, 10, 0, 0),
  8.                                           Text = "TestPage",
  9.                                           FontSize = 20
  10.                                       };
  11.             fixedPage.Children.Add(textBlock);
  12.             ((IAddChild)pageContent).AddChild(fixedPage);
  13.             dvDocumentViewer.Document = fixedDocument;

 

 

Saving a simple Fixed document Programmatically:

Code Snippet
  1. var xpsDocument = new XpsDocument(@"C:\test.xps", FileAccess.ReadWrite, CompressionOption.NotCompressed);
  2.          XpsDocumentWriter xdw = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
  3.          xdw.Write(document);
  4.          xpsDocument.Close();

 

 

Programmatically opening a Fixed document(XPS doc):

 

Code Snippet
  1. XpsDocument document = new XpsDocument(@"C:\Test.xps", FileAccess.Read);
  2.           IDocumentPaginatorSource documentPaginatorSource = document.GetFixedDocumentSequence();
  3.           dvDocumentViewer.Document = documentPaginatorSource;

 

 

 

Flow Documents snippets:

 

 

Code Snippet
  1. using System;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Windows.Documents;
  5. using System.Windows.Markup;
  6. using System.Windows.Media;
  7. using System.Windows.Shapes;
  8. using System.Xml;

 

Programmatically creating a Flow document(XAML):

 

Code Snippet
  1. var flowDocument = new FlowDocument();
  2.             //creating a new paragraph
  3.             var paragraph = new Paragraph();
  4.             paragraph.Inlines.Add("This is a simple para.");
  5.             paragraph.Inlines.Add("created in a flow document.");
  6.             paragraph.Inlines.Add("This is a para3");
  7.             paragraph.Inlines.Add("This is a para4");
  8.             paragraph.Inlines.Add("This is a para5");
  9.             paragraph.Inlines.Add("This is a para6");
  10.             paragraph.Inlines.Add("This is a para7");
  11.             //Adding para to blocks collection of flow document.
  12.             flowDocument.Blocks.Add(paragraph);
  13.             //Creating new floater object.
  14.             Floater floater = new Floater();
  15.             paragraph = new Paragraph();
  16.             Rectangle rect = new Rectangle();
  17.             rect.Width = 50;
  18.             rect.Height = 50;
  19.             rect.Fill = Brushes.Blue;
  20.             rect.StrokeThickness = 2;
  21.             rect.Stroke = Brushes.Black;
  22.             paragraph.Inlines.Add(rect);
  23.             floater.Blocks.Add(paragraph);
  24.             //Adding floater object to blocks collection of flow document.
  25.             flowDocument.Blocks.Add(new Paragraph(floater));
  26.             fdrViewer.Document = flowDocument;

 

 

Saving a simple Flow document Programmatically(XAML):

 

Code Snippet
  1. XmlTextWriter xmlWriter = null;
  2.             TextWriter writer = null;
  3.             Stream file = null;
  4.  
  5.             try
  6.             {
  7.                 file = File.Create(fileName);
  8.                 writer = new StreamWriter(file);
  9.                 xmlWriter = new XmlTextWriter(writer);
  10.                 //The main function of the XamlDesignerSerializationManager is to get and set the serialization mode for a given expression type.
  11.                 var xamlDesignerSerializationManager = new XamlDesignerSerializationManager(xmlWriter);
  12.                 XamlWriter.Save(documentPaginatorSource.DocumentPaginator.Source, xamlDesignerSerializationManager);
  13.             }

 

Programmatically opening a Flow document(XAML):

 

Code Snippet
  1. Stream file = null;
  2.          TextReader reader = null;
  3.          XmlTextReader xmlTextReader = null;
  4.          try
  5.          {
  6.              file = File.OpenRead(@"c:\test.xaml");
  7.              reader = new StreamReader(file);
  8.              xmlTextReader = new XmlTextReader(reader);
  9.  
  10.               FlowDocument flowDocument = XamlReader.Load(xmlTextReader) as FlowDocument;
  11.               fdv.Document = flowDocument;
  12.          }

Category: WPF