We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set ListLevel Indent & Paragraph's Left and Hanging Indents in Word Document | Java

there is my input document The paragrah OutlineLevel-new.docx (13.3 KB)

This is the document output I want output.docx (28.2 KB)

my code

Document doc = new Document(“The paragrah OutlineLevel-new.docx”);
com.aspose.words.List list = doc.getLists().add(ListTemplate.OUTLINE_LEGAL );
ListLevel level1 = list.getListLevels().get(0);
   
   level1.setNumberStyle(NumberStyle.ARABIC);

    level1.setNumberFormat("\u0000");

    ListLevel level2 = list.getListLevels().get(1);

    level2.setNumberStyle(NumberStyle.ARABIC);

    level2.setNumberFormat("\u0000.\u0001");

    level2.isLegal(true);

    ListLevel level3 = list.getListLevels().get(2);

    level3.setNumberStyle(NumberStyle.ARABIC);

    level3.setNumberFormat("\u0000.\u0001.\u0002");

    level3.isLegal(true);


    ListLevel level4 = list.getListLevels().get(3);

    level4.setNumberStyle(NumberStyle.ARABIC);

    level4.setNumberFormat("\u0000.\u0001.\u0002.\u0003");

    level4.isLegal(true);

    ListLevel level5 = list.getListLevels().get(4);

    level5.setNumberStyle(NumberStyle.ARABIC);

    level5.setNumberFormat("\u0000.\u0001.\u0002.\u0003.\u0004");

    level5.isLegal(true);


    ListLevel level6 = list.getListLevels().get(5);

    level6.setNumberStyle(NumberStyle.ARABIC);

    level6.setNumberFormat("\u0000.\u0001.\u0002.\u0003.\u0004.\u0005");
    level6.isLegal(true);

      NodeCollection nodes = doc.getChildNodes(NodeType.PARAGRAPH, true);
      List<Paragraph>  paragraphs= (List<Paragraph>)StreamSupport.stream(nodes.spliterator(), false).filter(
        node -> ((Paragraph)node).getParagraphFormat().getOutlineLevel() < OutlineLevel.BODY_TEXT).collect(
        Collectors.toList());

    for (Paragraph para : paragraphs)

    {
        para.getListFormat().applyNumberDefault();
        para.getParagraphFormat().setLeftIndent(0.0d);
        if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_1)

        {

            para.getListFormat().setList(list);

            para.getListFormat().setListLevelNumber(0);
        }

        else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_2)

        {

            para.getListFormat().setList(list);

            para.getListFormat().setListLevelNumber(1);

        }  else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_3)

        {

            para.getListFormat().setList(list);

            para.getListFormat().setListLevelNumber(2);

        }
        else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_4)

        {

            para.getListFormat().setList(list);

            para.getListFormat().setListLevelNumber(3);

        }
        else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_5)

        {

            para.getListFormat().setList(list);

            para.getListFormat().setListLevelNumber(4);

        }  else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_6)

        {

            para.getListFormat().setList(list);

            para.getListFormat().setListLevelNumber(5);
        }

    }


doc.save(  "/kkk1.docx");

@garyshen,

List<Paragraph> paragraphs = (List<Paragraph>) StreamSupport.stream(nodes.spliterator(), false).filter(
        node -> ((Paragraph) node).getParagraphFormat().getOutlineLevel() < OutlineLevel.BODY_TEXT).collect(
        Collectors.toList());

Can you please translate above statements into basic Java code because I am observing “Cannot resolve symbol StreamSupport and Collectors” and also the following message on my end:

Inconvertible types: cannot cast ‘’ to com.aspose.words.Paragraph (see Inconvertible types.png (8.8 KB))

It would be great if you please create a standalone simplified Java Application (source code that does not use lambda parameters and without compilation errors) to reproduce this issue on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the ZIP file size. Also, please provide Aspose.Words 21.9 generated DOCX file showing the undesired behavior here for our reference. Thanks for your cooperation.

@awais.hafeez awais.hafeez thanks you reply.
I already created a test java Application ,you can get from attachment. SampleTest.java.zip (1.3 KB)
The input document The paragrah OutlineLevel-new.docx (13.3 KB)

The document output I want desire_result.docx (15.1 KB)

I desire all the Multilevel Number is on left side.

The source code same attachment , as below:

import java.util.ArrayList;
import java.util.List;

import com.aspose.words.Document;
import com.aspose.words.ListLevel;
import com.aspose.words.ListTemplate;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.NumberStyle;
import com.aspose.words.OutlineLevel;
import com.aspose.words.Paragraph;

public class SampleTest {
    public static void main(String args[]) throws Exception{
        Document doc = new Document("The paragrah OutlineLevel-new.docx");

        com.aspose.words.List list = doc.getLists().add(ListTemplate.OUTLINE_LEGAL );


        ListLevel level1 = list.getListLevels().get(0);

        level1.setNumberStyle(NumberStyle.ARABIC);

        level1.setNumberFormat("\u0000");

        ListLevel level2 = list.getListLevels().get(1);

        level2.setNumberStyle(NumberStyle.ARABIC);

        level2.setNumberFormat("\u0000.\u0001");

        level2.isLegal(true);

        ListLevel level3 = list.getListLevels().get(2);

        level3.setNumberStyle(NumberStyle.ARABIC);

        level3.setNumberFormat("\u0000.\u0001.\u0002");
        level3.isLegal(true);



        ListLevel level4 = list.getListLevels().get(3);

        level4.setNumberStyle(NumberStyle.ARABIC);

        level4.setNumberFormat("\u0000.\u0001.\u0002.\u0003");

        level4.isLegal(true);

        ListLevel level5 = list.getListLevels().get(4);

        level5.setNumberStyle(NumberStyle.ARABIC);

        level5.setNumberFormat("\u0000.\u0001.\u0002.\u0003.\u0004");

        level5.isLegal(true);


        ListLevel level6 = list.getListLevels().get(5);

        level6.setNumberStyle(NumberStyle.ARABIC);

        level6.setNumberFormat("\u0000.\u0001.\u0002.\u0003.\u0004.\u0005");
        level6.isLegal(true);


        NodeCollection nodes = doc.getChildNodes(NodeType.PARAGRAPH, true);
        List<Paragraph> paragraphs=new ArrayList<>();
        for(int i=0;i<nodes.getCount();i++) {
            Paragraph node =(Paragraph)nodes.get(i);
            if(node.getParagraphFormat().getOutlineLevel()< OutlineLevel.BODY_TEXT){
                paragraphs.add(node);
            }
        }
        for (Paragraph para : paragraphs)

        {
            //set leftIndent all
            para.getParagraphFormat().setLeftIndent(0.0d);

            para.getListFormat().applyNumberDefault();
            if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_1)

            {

                para.getListFormat().setList(list);

                para.getListFormat().setListLevelNumber(0);
            }

            else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_2)

            {

                para.getListFormat().setList(list);

                para.getListFormat().setListLevelNumber(1);

            }  else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_3)

            {

                para.getListFormat().setList(list);

                para.getListFormat().setListLevelNumber(2);

            }
            else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_4)

            {

                para.getListFormat().setList(list);

                para.getListFormat().setListLevelNumber(3);

            }
            else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_5)

            {

                para.getListFormat().setList(list);

                para.getListFormat().setListLevelNumber(4);

            }  else if (para.getParagraphFormat().getOutlineLevel()==OutlineLevel.LEVEL_6)

            {

                para.getListFormat().setList(list);

                para.getListFormat().setListLevelNumber(5);
            }

        }


        doc.save(  "desire_result.docx");
    }
}

@garyshen,

Please add the following code before calling the save method:

...
...
...

for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true))
    if (para.isListItem())
        para.getParagraphFormat().setLeftIndent(-1 * para.getParagraphFormat().getFirstLineIndent());

doc.save(  "desire_result.docx");