<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
<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
<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; } } }
Programmatically add rows to a Table element.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication1.Window1" Title="Add Content to a Table"> <FlowDocumentScrollViewer HorizontalAlignment="Left" VerticalAlignment="Top"> <FlowDocument> <Paragraph> <Button Canvas.Left="5" Click="AddRow">Add a New TableRow to the Table</Button> </Paragraph> <Table CellSpacing="5" Name="table1"> <Table.Columns> <TableColumn/> </Table.Columns> <TableRowGroup Name="trg1"> <TableRow> <TableCell> <Paragraph FontSize="14pt">TableRow</Paragraph> </TableCell> </TableRow> </TableRowGroup> </Table> </FlowDocument> </FlowDocumentScrollViewer> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace WpfApplication1 { public partial class Window1 : Window { public void AddRow(object sender, RoutedEventArgs e) { TableRow row = new TableRow(); trg1.Rows.Add(row); Paragraph para = new Paragraph(); para.Inlines.Add("A new Row and Cell have been Added to the Table"); TableCell cell = new TableCell(para); row.Cells.Add(cell); } } }
Table Flow Content
<Window x:Class="Documents.FlowContent" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="FlowContent" Height="400" Width="600"> <FlowDocumentScrollViewer Name="docViewer" IsToolBarVisible="True" > <FlowDocument> <BlockUIContainer> <Button Click="cmdCreateDynamicDocument_Click" HorizontalAlignment="Left" Padding="5">Create a Dynamic Document</Button> </BlockUIContainer> <Paragraph FontSize="20pt">Largest Cities in the Year 100</Paragraph> <Table> <Table.Columns> <TableColumn Width="*"></TableColumn> <TableColumn Width="3*"></TableColumn> <TableColumn Width="*"></TableColumn> </Table.Columns> <TableRowGroup > <TableRow FontWeight="Bold" > <TableCell > <Paragraph>Title 1</Paragraph> </TableCell> <TableCell> <Paragraph>Title 2</Paragraph> </TableCell> <TableCell> <Paragraph>Title 3</Paragraph> </TableCell> </TableRow> <TableRow> <TableCell> <Paragraph>A</Paragraph> </TableCell> <TableCell> <Paragraph>A</Paragraph> </TableCell> <TableCell> <Paragraph>A</Paragraph> </TableCell> </TableRow> </TableRowGroup> </Table> </FlowDocument> </FlowDocumentScrollViewer> </Window> //File:Window.xaml.cs using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Documents { public partial class FlowContent : System.Windows.Window { public FlowContent() { InitializeComponent(); } private void cmdCreateDynamicDocument_Click(object sender, RoutedEventArgs e) { Run runFirst = new Run(); runFirst.Text = "A "; Bold bold = new Bold(); Run runBold = new Run(); runBold.Text = "bold "; bold.Inlines.Add(runBold); Run runLast = new Run(); runLast.Text = " documents"; Paragraph paragraph = new Paragraph(); paragraph.Inlines.Add(runFirst); paragraph.Inlines.Add(bold); paragraph.Inlines.Add(runLast); FlowDocument document = new FlowDocument(); document.Blocks.Add(paragraph); docViewer.Document = document; } } }
Annotation Service
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:a="clr-namespace:System.Windows.Annotations;assembly=PresentationFramework" Title="Flow Document Reader + Annotations" x:Class="Window1" Initialized="OnInitialized" Closed="OnClosed"> <StackPanel> <StackPanel Orientation="Horizontal"> <Label>Control Annotations:</Label> <Button Command="a:AnnotationService.CreateTextStickyNoteCommand" CommandTarget="{Binding ElementName=reader}">Create Text Note</Button> <Button Command="a:AnnotationService.CreateInkStickyNoteCommand" CommandTarget="{Binding ElementName=reader}"> Create Ink Note </Button> <Button Command="a:AnnotationService.DeleteStickyNotesCommand" CommandTarget="{Binding ElementName=reader}"> Remove Note </Button> <Button Command="a:AnnotationService.CreateHighlightCommand" CommandTarget="{Binding ElementName=reader}"> Create Yellow Highlight </Button> <Button Command="a:AnnotationService.ClearHighlightsCommand" CommandTarget="{Binding ElementName=reader}"> Remove Highlight </Button> </StackPanel> <FlowDocumentReader x:Name="reader"> <FlowDocument> <Paragraph FontSize="22" FontWeight="Bold">Chapter 1</Paragraph> <Paragraph FontSize="35" FontWeight="Bold">Why WPF?</Paragraph> <Paragraph> this is a test this is a test this is a test this is a test this is a test this is a test this is a test </Paragraph> <Paragraph> this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test </Paragraph> <Paragraph> this is another test </Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> <Paragraph>...</Paragraph> </FlowDocument> </FlowDocumentReader> </StackPanel> </Window> //File:Window.xaml.cs using System; using System.IO; using System.Windows; using System.Windows.Annotations; using System.Windows.Annotations.Storage; public partial class Window1 : Window { Stream stream; public Window1() { InitializeComponent(); } protected void OnInitialized(object sender, EventArgs e) { AnnotationService service = AnnotationService.GetService(reader); if (service == null) { stream = new FileStream("storage.xml", FileMode.OpenOrCreate); service = new AnnotationService(reader); AnnotationStore store = new XmlStreamStore(stream); service.Enable(store); } } protected void OnClosed(object sender, EventArgs e) { AnnotationService service = AnnotationService.GetService(reader); if (service != null && service.IsEnabled) { service.Store.Flush(); service.Disable(); stream.Close(); } } }
Toggle Hyphenation, Optimal Paragraph, and Column Flex
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="FlowDocument Optimal Paragraph Sample"> <StackPanel> <CheckBox Click="ToggleHyphenation" Content="Automatic Hyphenation"/> <CheckBox Click="ToggleOptimalParagraph" Content=" Enable Optimal Paragraph Layout"/> <CheckBox Click="ToggleColumnFlex" Content="Enable Flexible Columns"/> <Slider Name="columnWidthSlider" ValueChanged="ChangeColumnWidth"/> <Slider Name="columnGapSlider" ValueChanged="ChangeColumnGap"/> <FlowDocumentReader Name="flowReader"> <FlowDocument Name="flowDoc" TextAlignment="Justify" IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True" IsColumnWidthFlexible="True" Background="AliceBlue" ColumnWidth="300" ColumnGap="20"> <Paragraph><Italic>this is a test</Italic></Paragraph> <Paragraph> <Hyperlink NavigateUri="http://www-cs-faculty.stanford.edu/~knuth/"> - Donald E. Knuth </Hyperlink> </Paragraph> <Paragraph><Bold>Principle of Optimal Paragraph</Bold></Paragraph> <Paragraph>this is a test <Italic>italic</Italic> </Paragraph> <Paragraph>this is a test</Paragraph> <Paragraph>this is a test</Paragraph> <Paragraph>this is a test</Paragraph> </FlowDocument> </FlowDocumentReader> </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.Shapes; namespace WpfApplication1 { public partial class Window1 : Window { void ToggleHyphenation(Object sender, RoutedEventArgs args) { flowDoc.IsHyphenationEnabled = ((CheckBox)sender).IsChecked.Value; } void ToggleOptimalParagraph(Object sender, RoutedEventArgs args) { flowDoc.IsOptimalParagraphEnabled = ((CheckBox)sender).IsChecked.Value; } void ToggleColumnFlex(Object sender, RoutedEventArgs args) { flowDoc.IsColumnWidthFlexible = ((CheckBox)sender).IsChecked.Value; } void ChangeColumnWidth(Object sender, RoutedEventArgs args) { if (columnWidthSlider.Value == 0) { flowDoc.ColumnWidth = 100; } else if (columnWidthSlider.Value == 1) { flowDoc.ColumnWidth = 200; } else if (columnWidthSlider.Value == 2) { flowDoc.ColumnWidth = 300; } else if (columnWidthSlider.Value == 3) { flowDoc.ColumnWidth = 400; } else if (columnWidthSlider.Value == 4) { flowDoc.ColumnWidth = 500; } } void ChangeColumnGap(Object sender, RoutedEventArgs args) { if (columnGapSlider.Value == 0) { flowDoc.ColumnGap = 5; } else if (columnGapSlider.Value == 1) { flowDoc.ColumnGap = 10; } else if (columnGapSlider.Value == 2) { flowDoc.ColumnGap = 15; } else if (columnGapSlider.Value == 3) { flowDoc.ColumnGap = 20; } else if (columnGapSlider.Value == 4) { flowDoc.ColumnGap = 25; } } } }