FirstLineIndent modify LeftIndent when I set negative values

Hello,

I am wokring creating a word document and when I tried use FirstLineIndent with negative values (to set hanging) this also modify the LeftIndent value with the negative value. How I can set hanging in a paragraph and keep the value in Left Ident?


4 Replies 1 reply marked as answer

SB Sneha Biju Syncfusion Team February 5, 2024 12:16 PM UTC

Hi Juan,

We have reproduced the reported problem “LeftIndent is changed while setting negative value for FirstLineIndent in a Word document” in our end. We will validate this issue and update you with more details by mid of this week.

Regards,
Sneha.



SB Suriya Balamurugan Syncfusion Team February 7, 2024 02:13 PM UTC

Juan,

Upon further analysis, we have found that the hanging indent and left indent are interrelated at the file level when a hanging indent is set for a paragraph in a Word document.

As per DocIO behavior, if we set the hanging indent, we write the hanging indent and left indent as they are in the Word document at the file level. However, Microsoft Word automatically calculates the left indent value based on the hanging indent value and shows in UI. This is the hanging indentation behavior of Microsoft Word.

UI level

File level


To achieve your requirement of setting a hanging indent for a paragraph, we suggest setting the hanging indent along with the required left indent value using the custom method provided below. This custom method sets the given hanging indent value and converts the left indent value based on the hanging indent value.

Please refer to the code snippet below for setting the hanging indent along with the left indent of the paragraph.


WordDocument document = new WordDocument();

document.EnsureMinimal();

document.LastParagraph.AppendText("Hello");

WParagraph nextPara = document.LastSection.AddParagraph() as WParagraph;

nextPara.AppendText("World");

//Set the hanging and left indent for the paragraph.

//If need left indent, set required left indent otherwise set 0f.

SetHangingAndLeftindent(nextPara.ParagraphFormat, -10f, 0f);

document.Save(@"Output.docx");

document.Close();


/// <summary>

/// Set the hanging and left indent for the paragraph.

/// </summary>

/// <param name="paragraphFormat">The paragraph format.</param>

/// <param name="hangingIndent">The hanging indent UI value.</param>

/// <param name="leftIndent">The left indent UI value.</param>

private static void SetHangingAndLeftindent(WParagraphFormat paragraphFormat, float hangingIndent, float leftIndent)

{

    paragraphFormat.FirstLineIndent = hangingIndent;

    if (paragraphFormat.FirstLineIndent < 0)

    {

        paragraphFormat.LeftIndent = leftIndent + (-1 * paragraphFormat.FirstLineIndent);

    }

    else

        paragraphFormat.LeftIndent = leftIndent;

}


Regards,
Suriya Balamurugan.


Marked as answer

JU JUAN replied to Suriya Balamurugan February 7, 2024 04:05 PM UTC

Hi Suriya,

Its works very good, Thanks for the support!

I think this method will be included in the documentation for reference in the future.  



AA Akash Arul Syncfusion Team February 8, 2024 01:25 PM UTC

Juan, we are glad to know that it worked, and we have planned to document your requirement in our user guide.


Loader.
Up arrow icon