Draw justified text in C#

[justified text]

The StringFormat class has Alignment and LineAlignment properties that let you draw justified text. They let you specify how you want text aligned vertically and horizontally. The Alignment property determines the text’s horizontal alignment and the LineAlignment property determines its vertical alignment.

You can set these properties to one of the values defined by the StringAlignment enumeration. Those values are Near, Far, and Center.

Near aligns the text close to the top or left edge of the drawing area depending on whether you’re setting Alignment or LineAlignment. Far aligns the text close to the right or bottom edge of the drawing area. As you can probably guess, Center centers the text.

This example’s DrawText method, which is shown in the following code, draws some justified text in an indicated rectangle.

private void DrawText(Graphics gr, string text,
    Rectangle rect, StringAlignment alignment)
    gr.DrawRectangle(Pens.Blue, rect);
    using (StringFormat string_format = new StringFormat())
        // Center alignment.
        string_format.Alignment = alignment;
        string_format.FormatFlags = StringFormatFlags.LineLimit;
        string_format.Trimming = StringTrimming.Word;

        gr.DrawString(text, this.Font, Brushes.Black,
            rect, string_format);

The code draws the rectangle so you can see where it is drawing. It then creates a StringFormat object. Because the StringFormat class implements IDisposable, the code should call its Dispose method when it is done with the object. The using statement shown here automatically calls Dispose when the code exits the using block so the code doesn’t need to remember to do it.

The code sets the StringFormat object’s Alignment property to indicate how the justified text should be drawn. It then sets FormatFlags to LineLimit so the text doesn’t end with a line that is truncated vertically. (See the example Perform line clipping in C#.) It also sets the Trimming property so lines of text end at word boundaries rather than displaying part of a word or character at the end. (See the example Perform line trimming in C#.)

Finally the code calls the Graphics object’s DrawString method to draw the sample text.

The main program uses the following code to draw three samples of justified text showing each of the Alignment values.

// Left alignment.
Rectangle rect = new Rectangle(gap, gap, wid, hgt);
e.Graphics.DrawRectangle(Pens.Blue, rect);
DrawText(e.Graphics, text, rect, StringAlignment.Near);

// Right alignment.
rect.X += wid + gap;
e.Graphics.DrawRectangle(Pens.Blue, rect);
DrawText(e.Graphics, text, rect, StringAlignment.Far);

// Center alignment.
rect.X += wid + gap;
e.Graphics.DrawRectangle(Pens.Blue, rect);
DrawText(e.Graphics, text, rect, StringAlignment.Center);

Unfortunately the Alignment property doesn’t support “full” justification where the word spacing is adjusted to make the text completely fill each line except the last one in the paragraph.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in fonts, graphics and tagged , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Draw justified text in C#

  1. Omer says:

    Hey, I want my text to be printed with justified alignment. is there any way ?

Leave a Reply

Your email address will not be published. Required fields are marked *