Use FlowDocumentReader to display FlowDocument

image_pdfimage_print


   
  

<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;
        }
    }
}

   
    
     


Programmatically Create and Save a FlowDocument

image_pdfimage_print


   
  

<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;
        }
    }
}

   
    
     


Use XamlDesignerSerializationManager to write FlowDocument

image_pdfimage_print


   
  


<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;
        }
    }
}

   
    
     


Load a FlowDocument into a FlowDocumentReader,

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="FlowDocReader Load/Save Sample" Width="640" Height="480">
  <StackPanel>
    <FlowDocumentReader Name="FlowDocRdr" Grid.Row="1"/>
  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using System.Windows.Markup;

namespace WpfApplication1
{

    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();

            FlowDocument content = null;
            
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "FlowDocument Files (*.xaml)|*.xaml|All Files (*.*)|*.*";

            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileStream xamlFile = openFile.OpenFile() as FileStream;
                if (xamlFile == null) return;
                else
                {   
                    try 
                    { 
                        content = XamlReader.Load(xamlFile) as FlowDocument; 
                        if (content == null) 
                            throw(new XamlParseException("The specified file could not be loaded as a FlowDocument."));
                    }
                    catch (XamlParseException e)
                    {
                        String error = "There was a problem parsing the specified file:

";
                        error += openFile.FileName;
                        error += "

Exception details:

";
                        error += e.Message;
                        System.Windows.MessageBox.Show(error);
                        return;
                    }
                    catch (Exception e) 
                    {
                        String error = "There was a problem loading the specified file:

";
                        error += openFile.FileName;
                        error += "

Exception details:

";
                        error += e.Message;
                        System.Windows.MessageBox.Show(error);
                        return;
                    }

                    FlowDocRdr.Document = content;
                }
            }
        }
    }
}

   
    
     


Convert Xaml file to FlowDocument with as operator

image_pdfimage_print
   
  


<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="FlowDocReader Load/Save Sample" Width="640" Height="480">
  <StackPanel>
    <FlowDocumentReader Name="FlowDocRdr" Grid.Row="1"/>
  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using System.Windows.Markup;

namespace WpfApplication1
{

    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();

            FlowDocument content = null;
            
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "FlowDocument Files (*.xaml)|*.xaml|All Files (*.*)|*.*";

            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileStream xamlFile = openFile.OpenFile() as FileStream;
                if (xamlFile == null) return;
                else
                {   
                    try 
                    { 
                        content = XamlReader.Load(xamlFile) as FlowDocument; 
                        if (content == null) 
                            throw(new XamlParseException("The specified file could not be loaded as a FlowDocument."));
                    }
                    catch (XamlParseException e)
                    {
                        String error = "There was a problem parsing the specified file:

";
                        error += openFile.FileName;
                        error += "

Exception details:

";
                        error += e.Message;
                        System.Windows.MessageBox.Show(error);
                        return;
                    }
                    catch (Exception e) 
                    {
                        String error = "There was a problem loading the specified file:

";
                        error += openFile.FileName;
                        error += "

Exception details:

";
                        error += e.Message;
                        System.Windows.MessageBox.Show(error);
                        return;
                    }

                    FlowDocRdr.Document = content;
                }
            }
        }
    }
}

   
    
     


Save a FlowDocument as a Extensible Application Markup Language (XAML) file

image_pdfimage_print
   
  



<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="FlowDocReader Load/Save Sample" Width="640" Height="480">
  <StackPanel>
    <FlowDocumentReader Name="FlowDocRdr" Grid.Row="1"/>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using System.Windows.Markup;

namespace WpfApplication1
{

    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();

            SaveFileDialog saveFile = new SaveFileDialog();
            FileStream xamlFile = null;
            saveFile.Filter = "FlowDocument Files (*.xaml)|*.xaml|All Files (*.*)|*.*";
            if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    xamlFile = saveFile.OpenFile() as FileStream;
                }
                catch (Exception e)
                {
                    String error = "There was a opening the file:

";
                    error += saveFile.FileName;
                    error += "

Exception details:

";
                    error += e.Message;
                    System.Windows.MessageBox.Show(error);
                    return;
                }
                if (xamlFile == null) return;
                else
                {
                    XamlWriter.Save(FlowDocRdr.Document, xamlFile);
                    xamlFile.Close();
                }
            }
        }

    }
}

   
    
     


Clear FlowDocumentReader

image_pdfimage_print


   
  




<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="FlowDocReader Load/Save Sample" Width="640" Height="480">
  <StackPanel>
    <FlowDocumentReader Name="FlowDocRdr" Grid.Row="1"/>
  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using System.Windows.Markup;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            FlowDocRdr.Document = null; 
        }
    }
}