Click here to Skip to main content
11,927,422 members (46,774 online)
Click here to Skip to main content
Add your own
alternative version


68 bookmarked

Text On A Path in WPF

, 30 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A control that will display a text string on a specified geometry


Currently, with Window Presentation Foundation (WPF), it is not easy to display a text string that will follow the path of any given geometry. WPF’s TextEffects provides transformation on individual characters, but trying to use this capability to get the text to follow an arbitrary geometry requires some additional work. Also, TextEffects is not available in Silverlight. With the control provided here, a given text string can be made to display on any 2D WPF geometry. The control can, for example, display text on an EllipseGeometry:


A sample application and source code has been provided.


To display text on a path, the fundamental problem that must be solved is to figure out how to translate and rotate each character of the string to fit the specified geometry. A big help in solving the problem is the GetFlattenedPathGeometry() method provided by the Geometry base class. This method returns a PathGeometry that approximates the geometry by a series of lines (PolyLineSegment and LineSegment). For example, the red lines below depict the flattened path of the path geometry (black):


For a good overview of the WPF geometry classes, see MSDN.

The next step in the problem is to take the flattened path geometry and figure out how to transform the characters of the string to follow the flattened path. The figure below depicts the angle that must be calculated so the letter “A” will follow the flattened path. In TextOnAPath, the method ‘GetIntersectionPoints’ in the GeometryHelper class calculates the points along the flattened path where the characters intersect with the path. Knowing the intersection points, the angle (and translation) values can then be calculated.


Using the Code

The TextOnAPath control is used just like any normal UIElement in WPF. The XAML below will display the text string along the curved path, centered in a grid:

<Window x:Class="TextOnAPathTestApp.Window1"


    Title="Test App" Height="500" Width="500">
    <TextOnAPath:TextOnAPath FontSize="30" DrawPath="True" 
        Text="The quick brown fox jumped over the lazy hen.">
                <PathGeometry Figures="M0,0 C120,361 230.5,276.5 230.5,276.5 
                      L308.5,237.50001 C308.5,237.50001 419.5,179.5002 367.5,265.49993 
                      315.5,351.49966 238.50028,399.49924 238.50028,399.49924 L61.500017,


Parameters for the TextOnAPath control:

  • Text (string): The string to be displayed. If the text string is longer than the geometry path, then text will be truncated.
  • TextPath (Geometry): The geometry for the text to follow.
  • DrawPath (Boolean): If true, draws the geometry below the text string.
  • DrawLinePath (Boolean): If true, draws the flattened path geometry below the text string.
  • ScaleTextPath (Boolean): If true, the geometry will be scaled to fit the size of the control.

Points of Interest


  • 13th October, 2008
    • Initial post
  • 29th October, 2008
    • Update to fix code that draws geometry
    • Provided link to Silverlight version of code


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Skype
United States United States
I work in the Bay Area primarily developing software on the Windows platform using C++, .NET/C#, WPF, and Silverlight.

You may also be interested in...

Comments and Discussions

QuestionHow to make this work from code? Pin
Kyudos9-Jan-14 13:13
memberKyudos9-Jan-14 13:13 
AnswerRe: How to make this work from code? Pin
Kyudos9-Jan-14 13:30
memberKyudos9-Jan-14 13:30 
QuestionAlignment of the text Pin
Neelendu Kumar24-Sep-13 4:29
memberNeelendu Kumar24-Sep-13 4:29 
AnswerRe: Alignment of the text Pin
Kyudos19-Dec-13 16:54
memberKyudos19-Dec-13 16:54 
GeneralRe: Alignment of the text Pin
Kyudos22-Dec-13 13:59
memberKyudos22-Dec-13 13:59 
AnswerRe: Alignment of the text Pin
Kyudos23-Dec-13 10:46
memberKyudos23-Dec-13 10:46 
GeneralRe: Alignment of the text Pin
Neelendu Kumar24-Dec-13 2:18
memberNeelendu Kumar24-Dec-13 2:18 
Thanks ..
QuestionAlternative for RenderTransformOrigin ...??? Pin
patelmayur15-Jun-12 4:55
memberpatelmayur15-Jun-12 4:55 
AnswerRe: Alternative for RenderTransformOrigin ...??? Pin
lneir19-Jun-12 20:08
memberlneir19-Jun-12 20:08 
Questiondraw text within any path? Pin
ASPUIPAB29-Oct-11 12:37
memberASPUIPAB29-Oct-11 12:37 
AnswerRe: draw text within any path? Pin
lneir30-Oct-11 8:00
memberlneir30-Oct-11 8:00 
GeneralRe: draw text within any path? Pin
Member 83727423-Nov-11 8:10
memberMember 83727423-Nov-11 8:10 
GeneralRe: draw text within any path? Pin
lneir3-Nov-11 8:26
memberlneir3-Nov-11 8:26 
AnswerText on EllipseGeometry Pin
ANTONELLO7221-May-09 1:57
memberANTONELLO7221-May-09 1:57 
General'hen' -&gt; 'dog' Pin
NigelHome20-Oct-08 22:26
memberNigelHome20-Oct-08 22:26 
GeneralRe: 'hen' -&gt; 'dog' Pin
lneir21-Oct-08 6:26
memberlneir21-Oct-08 6:26 
GeneralRe: 'hen' -&gt; 'dog' Pin
Richard Deeming22-Oct-08 5:06
memberRichard Deeming22-Oct-08 5:06 
GeneralRe: 'hen' -&gt; 'dog' Pin
kalme22-Oct-08 9:16
memberkalme22-Oct-08 9:16 
GeneralRe: 'hen' -&gt; 'dog' Pin
Member 46471625-Nov-08 0:35
memberMember 46471625-Nov-08 0:35 
GeneralPath Flattening Pin
kevingay20-Oct-08 15:42
memberkevingay20-Oct-08 15:42 
GeneralEasier way Pin
Daniel Larocque14-Oct-08 3:56
memberDaniel Larocque14-Oct-08 3:56 
GeneralRe: Easier way Pin
lneir14-Oct-08 6:41
memberlneir14-Oct-08 6:41 
GeneralRe: Easier way Pin
mike castillo21-Oct-08 7:11
membermike castillo21-Oct-08 7:11 
GeneralRe: Easier way Pin
lneir21-Oct-08 7:32
memberlneir21-Oct-08 7:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 30 Oct 2008
Article Copyright 2008 by lneir
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid