Programmatically Create and Save a 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.vb
Imports System
Imports System.IO
Imports System.Windows
Imports System.Windows.Documents
Imports System.Windows.Markup
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Xml
Imports Microsoft.Win32
Namespace WpfApplication1
Public Partial Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnSave_Click(sender As Object, e As RoutedEventArgs)
SaveFile("c:\", fdrViewer.Document)
End Sub
Private Sub SaveFile(fileName As String, documentSource As IDocumentPaginatorSource)
Dim xmlWriter As XmlTextWriter = Nothing
Dim writer As TextWriter = Nothing
Dim file__1 As Stream = Nothing
Try
file__1 = File.Create(fileName)
writer = New StreamWriter(file__1)
xmlWriter = New XmlTextWriter(writer)
Dim xamlManager As New XamlDesignerSerializationManager(xmlWriter)
XamlWriter.Save(documentSource.DocumentPaginator.Source, xamlManager)
Catch e As Exception
Dim msg As String = String.Format("Error occurred during saving.{0}{0}{1}", Environment.NewLine, e.Message)
MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
End Try
End Sub
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Dim flowDocument As New FlowDocument()
Dim paragraph As New Paragraph()
paragraph.Inlines.Add("This is a paragraph.")
flowDocument.Blocks.Add(paragraph)
flowDocument.Blocks.Add(paragraph)
fdrViewer.Document = flowDocument
End Sub
End Class
End Namespace
Related examples in the same category