Add Shape(Ellipse, Rectangle) to List

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="FlowDocumentPage" Height="450" Width="500">
    <DockPanel>
        <Button Padding="20" Margin="40">
            <FlowDocumentReader>
                <FlowDocument FontSize="12" xml:space="preserve">
                    <Paragraph TextAlignment="Center" FontSize="18"><Bold>bold</Bold></Paragraph>
                    <Paragraph>Para<Italic>italic</Italic>HTML</Paragraph>
                    <Paragraph>aaa<Button>Push me</Button> 
                    <RadioButton IsChecked="True">Yes</RadioButton>
                    <RadioButton>No</RadioButton>
                    <RadioButton>Unsure</RadioButton>
                    </Paragraph>
                    <Paragraph><Bold>Lists:</Bold></Paragraph>
                    <List>
                        <ListItem><Paragraph>A</Paragraph></ListItem>
                        <ListItem><Paragraph>B</Paragraph></ListItem>
                        <ListItem><Paragraph>C<Ellipse Fill="Red" Width="20" Height="20"/></Paragraph></ListItem>
                        <ListItem><Paragraph>D<Rectangle Fill="Blue" Width="20" Height="20"></Rectangle></Paragraph></ListItem>
                    </List>
                </FlowDocument>
            </FlowDocumentReader>
        </Button>
    </DockPanel>
</Window>

   
    
    
    
    
    
    
     


Change FlowDocument Width and Height

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

        }
    }
}

   
    
     


Programmatically change the FlowDirection of content within a FlowDocumentReader element

image_pdfimage_print


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.Window1" Title="FlowDirection Sample">

    <DockPanel HorizontalAlignment="Left" VerticalAlignment="Top">
      <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,0,0,10">
        <Button Click="LR">LeftToRight</Button>
        <Button Click="RL">RightToLeft</Button>
      </StackPanel>
      <TextBlock Name="txt1" DockPanel.Dock="Bottom" Margin="0,50,0,0"/>
      <FlowDocumentReader>
        <FlowDocument FontFamily="Arial" Name="tf1" >
          <Paragraph>this is a test</Paragraph>
          <Paragraph>this is a test</Paragraph>
      </FlowDocument>
      </FlowDocumentReader>
    </DockPanel>
</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 LR(object sender, RoutedEventArgs e)
        {
            tf1.FlowDirection = FlowDirection.LeftToRight;
            txt1.Text = "FlowDirection is now " + tf1.FlowDirection.ToString();
        }
        public void RL(object sender, RoutedEventArgs e)
        {
            tf1.FlowDirection = FlowDirection.RightToLeft;
            txt1.Text = "FlowDirection is now " + tf1.FlowDirection.ToString();
        }
    }
}

   
    
     


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