How to: Save the Contents of a FlowDocumentScrollViewer as a XAML File

This example demonstrates how to save the contents of a FlowDocumentScrollViewer (represented by the Document property) as a XAML file.

Example

The following example defines an empty, named FlowDocumentScrollViewer that will be manipulated by the code example below.

<FlowDocumentScrollViewer
  Name="flowDocScrollViewer" 
  HorizontalScrollBarVisibility="Auto" 
  VerticalScrollBarVisibility="Auto" 
  IsSelectionEnabled="True" 
  IsToolBarVisible="True" 
  MinZoom="50" MaxZoom="1000"
  Zoom="120" ZoomIncrement="5"
/>

To save the contents of the FlowDocumentScrollViewer to a file, open or create the file stream and use the Save method provided by the XamlWriter class to write the FlowDocument to the file stream.

The following example performs these steps.

void SaveFlowDocumentScrollViewerWithXAMLFile(string fileName)
{
    // Open or create the output file.
    FileStream xamlFile = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
    // Save the contents of the FlowDocumentReader to the file stream that was just opened.
    XamlWriter.Save(flowDocScrollViewer.Document, xamlFile);

    xamlFile.Close();
}

See Also

Tasks

How to: Load a XAML File into a FlowDocumentScrollViewer