How to create a Hanging indentation for a paragraph

Problem

In MS Word you can create a paragraph that has a hanging indentation. A hanging indentation means that the entire paragraph is indented except for the first line:

In Word this is created through the Paragraph dialog:

To get to this dialog in Word 2010/2007, select the paragraph you want to format > right click > select Paragraph. To get to this dialog in Word 2003, select the paragraph you want to format > Format menu > select Paragraph.

This post covers how to do this with WordWriter.

Solution

Though there isn’t a property for hanging indentation in the current version of WordWriter, if a hanging indentation is set in an input file or template the hanging indentation will be preserved.

It is also possible to create a hanging indentation using WordApplication by setting the indent property of the ParagraphFormatting object twice with SetIndent:

Note: The indentation is set using the Twips measurement unit (Twip = 1/1440 Inch). To simplify the use of Twips WordWriter provides a TwipsConverter object that allows you to convert twips to or from inches, centimeters and points.

The code below creates a 1 inch hanging indentation. Notice the negative value given to the FirstLine indentation:

ParagraphFormatting pFormat = doc.CreateParagraphFormatting();
//using the TwipsConverter to convert the inches into Twips
int TwipIndentVal = TwipsConverter.FromInches(1);


//setting the paragraph to be indented 1 inch left
pFormat.set_Indent(TwipIndentVal, ParagraphFormatting.IndentLocation.Left);


//The negative value takes back the first line by 1 inch, to the left margin
pFormat.set_Indent(-TwipIndentVal, ParagraphFormatting.IndentLocation.FirstLine);
Paragraph par = doc.InsertParagraphAfter(null, pFormat);

Related posts: