Mega Code Archive

 
Categories / C# / WPF
 

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

<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:\n\n";                     error += saveFile.FileName;                     error += "\n\nException details:\n\n";                     error += e.Message;                     System.Windows.MessageBox.Show(error);                     return;                 }                 if (xamlFile == null) return;                 else                 {                     XamlWriter.Save(FlowDocRdr.Document, xamlFile);                     xamlFile.Close();                 }             }         }     } }