This documentation is for the old version. Go to the latest Graphics Mill docs

GdiGraphics.DrawFormattedText Method (String, Font, SolidBrush, RectangleF, Boolean, Int32)

Draws formatted text according to its markup.

Namespace: Aurigma.GraphicsMill.Drawing
Assembly: Aurigma.GraphicsMill (in Aurigma.GraphicsMill.dll)


Visual Basic
Public Sub DrawFormattedText ( _
	s As String, _
	font As Font, _
	brush As SolidBrush, _
	rect As RectangleF, _
	clipping As Boolean, _
	tabSize As Integer _
public void DrawFormattedText(
	string s,
	Font font,
	SolidBrush brush,
	RectangleF rect,
	bool clipping,
	int tabSize



Type: System.String

Marked up text to draw. See Remarks section for details on text markup requirements.

Type: Aurigma.GraphicsMill.Drawing.Font

Font object specifying default text rendering options.

Type: Aurigma.GraphicsMill.Drawing.SolidBrush

SolidBrush object containing default font color.

Type: System.Drawing.RectangleF

Destination rectangle.

Type: System.Boolean

Value specifying whether to clip the text when it does not fit the bounding rectangle. If this value is false all the lines of the text will be rendered regardless of the destination rectangle.

Type: System.Int32

Value which specifies the number of whitespaces to replace one tabulation character in the text.


This method is quite similar to DrawText() with the exception of one difference. The text you want to draw using the DrawFormattedText() method must be marked up with XML-like tags. The following tags and document structure are allowed:

  <para [style="..."]>
    [some text]
    <span [style="..."]>
      [some text]
      <span [style="..."]>
        [some text]
    <span [style="..."]></span>
  <para [style="..."]></para>

Let us consider it more detailed:

root is the root tag. It is exactly one all over the text and includes all characters inside. If you place some text ouside the <root></root> tags, the DrawFormattedText() will throw an exception.

para is a paragraph tag. All the text you want to draw should be divided into paragraphs (at least one) and each of them should be enclosed between <para> and </para> tags. Otherwise, the text will not be parsed and drawn. It supports the style attribute (see below) to specify some text and color properties of this paragraph.

span is a tag intended to define a new inline area. You can use it to highlight some text inside a paragraph. These tags can be embedded in the existing <span></span> tags. It supports the style attribute too.


All allowed tags are case insensitive, however, the name of every opening tag must have the same case as that of the corresponding closing tag. It means that <SpAn></SpAn> or <rOOt></rOOt> pairs will be parsed successfully, but pairs like <Para></parA> will cause an error.

The text layout mechanism used in the DrawFormattedText() method is based on CSS and uses style attributes to attach some font, color and text parameters to paragraphs and theirs parts. This attribute accepts a string contained style parameters and corresponded values. Its common syntax is shown below:

style="style-parameter1:value1; style-parameter2:value2; ...; style-parameterN:valueN"

The style attribute and its value are case insensitive, i.e. this style setting will be applied correctly: StyLe="fOnT-faMily: CoUriEr NeW; FoNt-siZe: 18; cOlOr:RgB(100,10,10)"

All supported parameters are described in the following table:

Name Description Examples

Specifies a typeface name of the font.

If font with specified typeface name is not found, system uses the first font which matches other settings (as usual it is "Arial").


Selects between normal and italic faces within a font family.




Selects the weight of the font. The values 100 to 900 form an ordered sequence, where each number indicates a weight that is at least as dark as its predecessor. The keyword normal is synonymous with 400, and bold is synonymous with 700.





Specifies the font size.



Specifies the text color. A numerical RGB specification is used. No keywords (such as red, white, etc) are supported.

style="color:rgb(200, 0, 0)"


Specifies the text alignment. The following alignment types are supported:

  • center aligns text along the center.
  • left aligns text along the left edge.
  • right aligns text along the right edge.
  • justify aligns text along the left and right edges.

If the justify alignment type is specified the last line of the text can be aligned using the textAlignLast parameter. This parameter can be one of the following values:

  • center aligns the last line of the text along the center.
  • left aligns the last line of the text along the left edge.
  • right aligns the last line of the text along the right edge.
  • justify aligns the last line of the text along the left and right edges.

If the textAlignLast parameter is not specified the last line of the text will be aligned along the left edge.


style="text-align:justify; textAlignLast:right"


Specifies the space between two adjacent lines of text.



The DrawFormattedText() supports the following XML predeclared entities:

  • &amp; & ampersand;
  • &lt; < less than;
  • &gt; > greater than.

This method parses the tabulation character (\t in C# and vbTab in VB) as a sequence of whitespaces. Number of whitespaces to replace one tabulation character in the text can be specified with the tabSize parameter.

The extent of the text drawn by this method can be measured using the Font.MeasureFormattedText method.


The DrawFormattedText(), unlike the DrawText(), does not support the newline character (\n in C# and vbNewLine in VB). To draw a string on a new line enclose it in <para></para> tags.

See Also