How to set the Indentation and hanging for the Hedding 1 and both should be dynamic

I am passing input for Indentation and hanging from UI. I used below code but its not working kindly help me asap.
Please find input and expected output word document.
Input_hedding1.docx (13.0 KB)
Expected_output_hedding1.docx (13.0 KB)

public static void Hedding1(Document doc)
{
    var LeftIndent = 0.15;
    var Hannging = -0.5;
    Style heading1 = doc.Styles[StyleIdentifier.Heading1];

    //heading1.ParagraphFormat.LeftIndent = ConvertUtil.InchToPoint(A);
    //heading1.ParagraphFormat.FirstLineIndent = ConvertUtil.InchToPoint(B);

    //please note  Indentation and hanging should be dynamic

    heading1.ParagraphFormat.FirstLineIndent = ConvertUtil.InchToPoint(Hannging);
    heading1.ParagraphFormat.LeftIndent = ConvertUtil.InchToPoint(LeftIndent) - heading1.ParagraphFormat.FirstLineIndent;
}

@Princeshivananjappa In your code you change only style formatting, but in your document heading paragraphs also have explicit formatting set, which override the formatting inherited from the style. So in your case, you should either set formatting explicitly to each heading paragraph or clear explicit formatting of the heading paragraphs. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

var LeftIndent = 0.15;
var Hannging = -0.5;
Style heading1 = doc.Styles[StyleIdentifier.Heading1];
heading1.ParagraphFormat.FirstLineIndent = ConvertUtil.InchToPoint(Hannging);
heading1.ParagraphFormat.LeftIndent = ConvertUtil.InchToPoint(LeftIndent) - heading1.ParagraphFormat.FirstLineIndent;

// Clear explicit formatting set to heading 1 paragraphs.
doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
    .ToList().ForEach(p => { p.ParagraphFormat.ClearFormatting(); p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; });

doc.Save(@"C:\Temp\out.docx");

This is a similar question you already asked earlier:
https://forum.aspose.com/t/how-to-enable-the-numbering-style-1-2-3-property/256545

@alexey.noskov you are suggested code is working individual but i tried below same thing but its not working kindly help me.
Note: I am giving input from UI. sometime LeftIndent =0.

public void Heading1Level1Indentation(RuleModel ruleModel, RuleBaseModel ruleBaseModel)
{
    ValueJSON valueJSON = Newtonsoft.Json.JsonConvert.DeserializeObject<ValueJSON>(ruleModel.ValueJSON);

    foreach (var value in valueJSON.value_list)
    {
        Style heading1 = ruleBaseModel.SourceDocument.Styles[StyleIdentifier.Heading1];

        if (value.name.ToUpper() == "HANGING" && value.status == "true")
        {
            if (heading1.ParagraphFormat.FirstLineIndent.ToString() != ConvertUtil.InchToPoint(Convert.ToDouble(value.label)).ToString())
            {
                heading1.ParagraphFormat.FirstLineIndent = ConvertUtil.InchToPoint(Convert.ToDouble(value.label));
            }
        }
        else if (value.name.ToUpper() == "INDENTATION" && value.status == "true")
        {
            if (heading1.ParagraphFormat.FirstLineIndent.ToString() != ConvertUtil.InchToPoint(Convert.ToDouble(value.label)).ToString())
            {
                heading1.ParagraphFormat.LeftIndent = ConvertUtil.InchToPoint(Convert.ToDouble(value.label)) - heading1.ParagraphFormat.FirstLineIndent;
                //  heading1.ParagraphFormat.LeftIndent = -heading1.ParagraphFormat.FirstLineIndent;
            }
        }
    }
}

@Princeshivananjappa In the provided code you do not clear explicit formatting applied to heading paragraphs so the values set in the style properties might be overridden by the explicit paragraphs’ formatting.

// Clear explicit formatting set to heading 1 paragraphs.
doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
    .ToList().ForEach(p => { p.ParagraphFormat.ClearFormatting(); p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; });

@alexey.noskov Below code is working only for Indentation kindly help me.

public void Heading1Level1Indentation(RuleModel ruleModel, RuleBaseModel ruleBaseModel)
{
    ValueJSON valueJSON = Newtonsoft.Json.JsonConvert.DeserializeObject<ValueJSON>(ruleModel.ValueJSON);

    foreach (var value in valueJSON.value_list)
    {
        Style heading1 = ruleBaseModel.SourceDocument.Styles[StyleIdentifier.Heading1];

        if (value.name.ToUpper() == "HANGING" && value.status == "true")
        {
            Style heading11 = ruleBaseModel.SourceDocument.Styles[StyleIdentifier.Heading1];
            heading11.ParagraphFormat.FirstLineIndent = ConvertUtil.InchToPoint(-Convert.ToDouble(value.label));

            // Clear explicit formatting set to heading 1 paragraphs.
            ruleBaseModel.SourceDocument.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
                .ToList().ForEach(p => { p.ParagraphFormat.ClearFormatting(); p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; });
        }
        else if (value.name.ToUpper() == "INDENTATION" && value.status == "true")
        {
            Style heading11 = ruleBaseModel.SourceDocument.Styles[StyleIdentifier.Heading1];
            heading11.ParagraphFormat.LeftIndent = ConvertUtil.InchToPoint(Convert.ToDouble(value.label)) - heading11.ParagraphFormat.FirstLineIndent;

            // Clear explicit formatting set to heading 1 paragraphs.
            ruleBaseModel.SourceDocument.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
                .ToList().ForEach(p => { p.ParagraphFormat.ClearFormatting(); p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; });
        }
    }
}

@Princeshivananjappa Unfortunately, it is not quite clear what exactly does not work and how to reproduce the problem. Could you please create a simple console application that will allow us to reproduce the problem and elaborate the problem in more details? We will check the issue and provide you more information.

@alexey.noskov My issue has been resolved thanks.

1 Like