Converting text to geometry

image_pdfimage_print


   
  
<Window x:Class="GlyphExamples.GlyphClipping"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Glyph Clipping" Height="300" Width="300">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="64">
      <Grid.LayoutTransform>
        <ScaleTransform ScaleX="4" ScaleY="4" />
      </Grid.LayoutTransform>
      <Button x:Name="button1" Content="Click" />
    </Grid>
</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;
using System.Threading;

namespace GlyphExamples
{
    public partial class GlyphClipping : System.Windows.Window
    {
        public GlyphClipping()
        {
            InitializeComponent();
            FormattedText text = new FormattedText("CLIP!",
                Thread.CurrentThread.CurrentUICulture,
                FlowDirection.LeftToRight,
                new Typeface("Gill Sans Ultra Bold"),
                20,
                Brushes.Black);

            Geometry textGeometry = text.BuildGeometry(new Point(0, 0));
            button1.Clip = textGeometry;
        }
    }
}

   
    
     


Use a PathGeometry object to highlight displayed text.

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="Using a Path Geometry to Highlight Text"
  Background="PowderBlue">

  <StackPanel>
      <Button Margin="10" Grid.Column="2" Grid.Row="0" FontSize="16" Click="OnDisplayTextClick">Display Text</Button>
    <Canvas Margin="20" Height="150">
      <Path Canvas.Top="15" Canvas.Left="15" Stroke="SteelBlue" StrokeThickness="3" Fill="LightSteelBlue" Name="path" />
      <Ellipse Canvas.Top="0" Canvas.Left="0" Width="30" Height="30">
        <Ellipse.Fill>
          <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
            <RadialGradientBrush.GradientStops>
              <GradientStop Color="Yellow" Offset="0.25" />
              <GradientStop Color="Transparent" Offset="1" />
            </RadialGradientBrush.GradientStops>
          </RadialGradientBrush>
        </Ellipse.Fill>

        <Ellipse.RenderTransform>
          <MatrixTransform />
        </Ellipse.RenderTransform>
        <Ellipse.Triggers>
          <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard x:Name="storyboard">
                <MatrixAnimationUsingPath 
                  x:Name="matrixAnimation"
                  Duration="0:00:40"
                  RepeatBehavior="Forever"
                  Storyboard.TargetProperty="RenderTransform.Matrix" />
              </Storyboard>
            </BeginStoryboard>
            </EventTrigger.Actions>
          </EventTrigger>
        </Ellipse.Triggers>
      </Ellipse>
    </Canvas>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {     
        public Window1()
        {
            InitializeComponent();
        }

        public void OnDisplayTextClick(object sender, EventArgs e)
        {
            FormattedText formattedText = new FormattedText(
                "asdf",
                CultureInfo.GetCultureInfo("en-us"),
                FlowDirection.LeftToRight,
                new Typeface("Verdana"),
                96,
                Brushes.Black);

            formattedText.SetFontWeight(FontWeights.Bold);

            Geometry geometry = formattedText.BuildGeometry(new Point(0, 0));

            PathGeometry pathGeometry = geometry.GetFlattenedPathGeometry();

            path.Data = pathGeometry;

            matrixAnimation.PathGeometry = pathGeometry;
        }
    }
}

   
    
     


FlowDocument with UI container

image_pdfimage_print


   
     

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <BlockUIContainer>
    <Viewbox>
      <StackPanel Orientation="Horizontal">
        <Image Source="c:image.jpg" Margin="5"/>
        <TextBlock VerticalAlignment="Center" Width="100" TextWrapping="Wrap">
          The technologies in the .NET Framework 3.0.
        </TextBlock>
      </StackPanel>
    </Viewbox>
  </BlockUIContainer>
</FlowDocument>

   
    
    
    
    
     


FlowDocument with Table

image_pdfimage_print


   
     

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Table CellSpacing="5" Padding="15" FontFamily="Segoe UI">
    <Table.Background>
      <LinearGradientBrush>
        <GradientStop Color="Yellow" Offset="0"/>
        <GradientStop Color="Orange" Offset="1"/>
      </LinearGradientBrush>
    </Table.Background>
    <Table.Columns>
      <TableColumn/>
      <TableColumn/>
      <TableColumn/>
      <TableColumn/>
    </Table.Columns>
    <TableRowGroup>
      <TableRow>
        <TableCell ColumnSpan="4" TextAlignment="Center">
          <Paragraph FontWeight="Bold">.NET Framework 3.0</Paragraph>
        </TableCell>
      </TableRow>
      <TableRow>
        <TableCell BorderBrush="Black" BorderThickness="2" Background="LightGray"
        TextAlignment="Center" LineHeight="70">
          <Paragraph FontWeight="Bold">WPF</Paragraph>
        </TableCell>
        <TableCell BorderBrush="Black" BorderThickness="2" Background="LightGray"
        TextAlignment="Center">
          <Paragraph FontWeight="Bold">WCF</Paragraph>
        </TableCell>
        <TableCell BorderBrush="Black" BorderThickness="2" Background="LightGray"
        TextAlignment="Center">
          <Paragraph FontWeight="Bold">WF</Paragraph>
        </TableCell>
        <TableCell BorderBrush="Black" BorderThickness="2" Background="LightGray"
        TextAlignment="Center">
          <Paragraph FontWeight="Bold">WCS</Paragraph>
        </TableCell>
      </TableRow>
      <TableRow>
        <TableCell BorderBrush="Black" BorderThickness="2" Background="LightGray"
        TextAlignment="Center">
          <Paragraph FontWeight="Bold">ADO.NET</Paragraph>
        </TableCell>
        <TableCell BorderBrush="Black" BorderThickness="2" Background="LightGray"
        TextAlignment="Center">
          <Paragraph FontWeight="Bold">ASP.NET</Paragraph>
        </TableCell>
        <TableCell BorderBrush="Black" BorderThickness="2" Background="LightGray"
        TextAlignment="Center">
          <Paragraph FontWeight="Bold">Windows Forms</Paragraph>
        </TableCell>
        <TableCell BorderBrush="Black" BorderThickness="2" Background="LightGray"
        TextAlignment="Center">
          <Paragraph FontWeight="Bold">...</Paragraph>
        </TableCell>
      </TableRow>
    </TableRowGroup>
  </Table>
</FlowDocument>

   
    
    
    
    
     


Span and FlowDoucment

image_pdfimage_print


   
     
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Paragraph>
    <Bold>bold</Bold>
    <Italic>italic</Italic>
    <Underline>underline</Underline>
    <Hyperlink>hyperlink</Hyperlink>
    <Span BaselineAlignment="Superscript">superscript</Span>
    <Span BaselineAlignment="Subscript">subscript</Span>
    <Span>
      <Span.TextDecorations>
        <TextDecoration Location="Strikethrough"/>
      </Span.TextDecorations>
      strikethrough
    </Span>
  </Paragraph>
</FlowDocument>

   
    
    
    
    
     


Paragraph FontWeight

image_pdfimage_print


   
     

<FlowDocument
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" FontFamily="Georgia"
  FontSize="20" PagePadding="30" Background="White" Name="fd1">
  <Paragraph>
    <Figure Width="Content" HorizontalAnchor="ContentLeft"
      VerticalAnchor="ContentTop" Margin="0,0,0,20" Padding="0">
      <Paragraph FontSize="35" Margin="0" FontWeight="Bold">asdf</Paragraph>
      <Paragraph Margin="0">asdf</Paragraph>
    </Figure>
    asdf
  </Paragraph>
  <Paragraph>
    asdf
    </Paragraph>
  <Paragraph>
    asdf
    </Paragraph>
  <Paragraph>
    asdf
    </Paragraph>
  <Paragraph FontWeight="Bold">
    asdf
    </Paragraph>
  <Paragraph>
    sasdf
    </Paragraph>
  <Paragraph>
    asdf
    </Paragraph>
  <Paragraph>
    asdf
    </Paragraph>
  <Paragraph FontWeight="Bold">
    The Middle Years
    </Paragraph>
  <Paragraph>

  </Paragraph>
  <Paragraph>
    asdf
    </Paragraph>
  <Paragraph>
    asdf
    </Paragraph>
</FlowDocument>