Hanging Indent

I am trying to create a heading in a document with a number followed by a title. I allow my user to specify the “indent” distance between the number and the title (_styleSheet.IndentFromNumbers in the code below). If the title wraps onto a second line then I want it to use a hanging indent and line up the second line of the title with the first line of the title - not with the left margin.

I can create this in directly in Word by simply entering a hanging indent for the paragaph.

I use the following code:

var paraFormat = docBuilder.ParagraphFormat;
var tabStop = new TabStop((double) _styleSheet.IndentFromNumbers);
paraFormat.TabStops.Add(tabStop);
paraFormat.FirstLineIndent = -(double) _styleSheet.IndentFromNumbers; 
textToWrite = string.Format("{0}{1}{2}{3}", number, NumberTitleSeparator, ControlChar.Tab, title);
docBuilder.Writeln(textToWrite);

I am using a negative value for FirstLineIndent to create a hanging indent as described in your documentation. However this does not work. The second line of text is always aligned back with the left margin.

When I investigate the paragraph formatting in the generated Word document I find that I have a negative left indent value and a matching positive hanging indent value. I can manually make it work by setting the left indent value to zero and deleting the previous empty paragraph.

Am I doing something wrong here?

Thanks,

David

I have attached a sample of a generated document.

You can see the problem in any of the headings e.g.:

  1. DECLARATION OF OPENING/ANNOUNCEMENT OF VISITORS
  2. RECORD OF ATTENDANCE / APOLOGIES / LEAVE OF ABSENCE PREVIOUSLY APPROVED
  3. RESPONSE TO PREVIOUS PUBLIC QUESTIONS TAKEN ON NOTICE

Hi David,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.

  • Please create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate your output document

  • Please attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will investigate as to how you are expecting your final document be generated like.

Unfortunately, it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to reproduce the problem. As soon as you get these pieces of information to us we’ll start our investigation into your issue.

Hi Tahir,

Thanks for your reply.

I have attached a very simple project illustrating the problem.

I have also attached a Word document displaying how I want things to appear (test_fixed.docx). The only change I made in Word to test_fixed.docx from the document generated by my sample program was to change the LeftIndent for the “Title” paragraphs from -42.5 pt to 0 pt. (I tried to do this in code but it did not work - the left indent still came out as the negative value).

Thanks,

David

Hi David,

Thanks for sharing detail. I have modified the WriteTitle method. Please check the following highlighted code snippet. Please let us know if you have any more queries.

static void WriteTitle(string number, string title)
{
    const string numberTitleSeparator = ".";
    var paraFormat = _docBuilder.ParagraphFormat;
    _docBuilder.Font.Bold = true;
    paraFormat.TabStops.Add(new TabStop(_indentFromNumbers));
    paraFormat.FirstLineIndent = _indentFromNumbers;
    paraFormat.FirstLineIndent = -_indentFromNumbers; // a negative value here creates a hanging indent
    paraFormat.LeftIndent = _indentFromNumbers;
    var textToWrite = string.Format("{0}{1}{2}{3}", number, numberTitleSeparator, ControlChar.Tab, title);
    _docBuilder.Writeln(textToWrite);
}

Hello Tahir,

Thanks for your reply. That fixed my problem.

I found that it also worked if I commented out the first of your highlighted lines:

paraFormat.FirstLineIndent = _indentFromNumbers;

This line seems redundant - why is it necessary?

Thanks,

David

Hi David,

Thanks for your inquiry. Yes, there is no need of this line of code. You can remove this line.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.