Use FlowDocumentReader to display FlowDocument
<Window
x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<DockPanel>
<Button DockPanel.Dock="Bottom" Content="Save..." Click="btnSave_Click"/>
<FlowDocumentReader x:Name="fdrViewer" />
</DockPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Xml;
using Microsoft.Win32;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
SaveFile("c:\\", fdrViewer.Document);
}
private void SaveFile(string fileName,IDocumentPaginatorSource documentSource)
{
XmlTextWriter xmlWriter = null;
TextWriter writer = null;
Stream file = null;
try
{
file = File.Create(fileName);
writer = new StreamWriter(file);
xmlWriter = new XmlTextWriter(writer);
XamlDesignerSerializationManager xamlManager = new XamlDesignerSerializationManager(xmlWriter);
XamlWriter.Save(documentSource.DocumentPaginator.Source, xamlManager);
}
catch (Exception e)
{
string msg = string.Format("Error occurred during saving.{0}{0}{1}",
Environment.NewLine,
e.Message);
MessageBox.Show(msg,"Error",MessageBoxButton.OK,MessageBoxImage.Error);
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
FlowDocument flowDocument = new FlowDocument();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add("This is a paragraph.");
flowDocument.Blocks.Add(paragraph);
flowDocument.Blocks.Add(paragraph);
fdrViewer.Document = flowDocument;
}
}
}
Related examples in the same category