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

        }
    }
}