How to customize bullets and Text spacing while we create Word Document using aspose.word

Hi Team,
For our project we are creating the word docs using aspose.word
Kindly advise how to customize spacing(i.e spacing between bullets and text and between two lines ) while we create Word Document or after we have created word document.

Appreciate for soon reply.

Thanks

Regards
Sonam

Hi Team,

Any update on this?

Thanks
Sonam

Hi Tahir,

Thanks for above info.This is really helpfull.

Please help me with below queries also as I am not finding the methods for same in list or paragraph class.

  1. Method to set : spacing between two lines one is bulleted and other is non bulleted?
    Example :
    This is non bulleted line
  • This is bulleted line

Need to customize spacing between them


  1. Method to set : spacing between two lines both having different types of bullets?
    Example:
    - This is line with hash bullet
    · This is line with circled bullet.
    Need to customize spacing between them

Both of these spaces need to be different so require customizatin.Please help.

Thanks
Waiting for soon reply.

Regards
Sonam

Hi Tahir,
Also please suggest what level2.setTabPosition(100) method doing .As I got form api :
setTabPosition(doublevalue) : Returns or sets the tab position (in points) for the list level.

Is this method sets the spacing between bullet and text or its does something else??

if not ,which method is responsible for customizing the spacing between text and bullet?

Thanks
Sonam

Hi Sonam,
Please check Paragraph Format and List Format in Specifying Formatting topic. You can use setSpaceAfter to set space between list items and text. You can also use setLineSpacing and setLinceSpacingRule methods to set space between lines. Please check ParagraphFormat class for more details.
Best Regards,

hi,

I am using the code as below.

My Input word file ,ActualWordfile and expectedWordfile is attahced.

Please help and suggest how to get the expected word file. i.e I want that all level of bulleting should be moved by same level given.How is that possible?If I am using ListLevel level1 = list1.getListLevels().get(1);ListLevel level1 = list1.getListLevels().get(2);ListLevel level1 = list1.getListLevels().get(3);it works upto leve 2 only.Please suggest.

public class ReadWord {
    public static void main(String[] args) throws Exception {
        Document doc = new Document("Input.doc");
        for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH , true))
        {
            if(para.isListItem())
            {
                List list1 = para.getListFormat().getList();
                ListLevel level1 = list1.getListLevels().get(0);
                level1.setTabPosition(100); // list and text spacing - need to set to 0.12inches
            }
        }
        doc.save("D://Test//ActualOutputWord.doc");
    }
}

Thanks
Sonam

Hi muhammad

getBidi() method of Paragraph format class is not working properly.
This method should throw true when the paragraph has right paragraph in it.
But not working out.

Please check and assist.

Thanks
Sonam

Hi Sonam,

Thanks for your inquiry.

*sonam.arora:

My Input word file ,ActualWordfile and expectedWordfile is attahced.
Please help and suggest how to get the expected word file. i.e I want that all level of bulleting should be moved by same level given.How is that possible?*

Please use ListLevel.NumberPosition Property to set the position (in points) of the number or bullet for the list level. Please use the following code example to achieve your requirements.

Document doc = new Document(MyDir + "InputWord.doc");
List list1 = doc.getLists().get(0);
ListLevel level1 = list1.getListLevels().get(0);
level1.setNumberPosition(18);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list2 = doc.getLists().get(1);
level1 = list2.getListLevels().get(0);
level1.setNumberPosition(72);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list3 = doc.getLists().get(2);
level1 = list3.getListLevels().get(0);
level1.setNumberPosition(108);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list4 = doc.getLists().get(3);
level1 = list4.getListLevels().get(0);
level1.setNumberPosition(108);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
doc.updateListLabels();
doc.save(MyDir + "Out.docx"); 

*sonam.arora:

getBidi() method of Paragraph format class is not working properly.
This method should throw true when the paragraph has right paragraph in it.
But not working out.*

Please use the ParagraphFormat.Alignment property to get or set text alignment for the paragraph. If you still face any issue, please share your input document along with expected output document. We will then provide you more information about your query along with code.

The Font.Bidi property specifies whether the contents of this run shall have right-to-left characteristics. The Bidi property, when on, shall not be used with strongly left-to-right text. Any behavior under that condition is unspecified. This property, when off, shall not be used with strong right-to-left text. Any behavior under that condition is unspecified.

Hi Tahir,
Thanks for reply,
The above code is helpful.But it works only upto three multi levels.As soon as I add one more level and change the code for same as below, the indentation is handle upto list3 only.

List list1 = doc.getLists().get(0);
ListLevel level1 = list1.getListLevels().get(0);
level1.setNumberPosition(18);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list2 = doc.getLists().get(1);
level1 = list2.getListLevels().get(0);
level1.setNumberPosition(72);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list3 = doc.getLists().get(2);
level1 = list3.getListLevels().get(0);
level1.setNumberPosition(108);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list4 = doc.getLists().get(3);
level1 = list4.getListLevels().get(0);
level1.setNumberPosition(108);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list5 = doc.getLists().get(4);
level1 = list5.getListLevels().get(0);
level1.setNumberPosition(108);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
doc.updateListLabels();
doc.save("D://Test//OutputWord-aspose.doc");

Attached the Input and Output Word file.

Kindly advise.

Thanks
Sonam Arora

Hi Tahir,

Is there any method throgh which i can get that whether the next paragraph has different bullet than the previos one.
I havent find any in paragraph format or listformat class.

Please assist.

Thanks
Sonam Arora

Hi Tahir,

I am solved with the above issue.However facing below error as I try to get 10th index.

that when below code is added.:

List list11 = doc.getLists().get(10);
level1 = list11.getListLevels().get(0);
level1.setNumberPosition(64);
level1.setTabPosition(90);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);

I get the below error.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.aspose.words.ListCollection.get(Unknown Source)

Is aspose able to handle indents only upto 9 levels?

Thanks
Sonam Arora

Hi Tahir,


public class Sample {
    public static void main(String[] args) throws Exception {
        Document doc = new Document("D://Test//InputWord.doc");
        int count = 0;
        int i = 0;
        int j = 8;
        for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH , true))
        {
            if(para.isListItem())
            {
                count++;
                final int one = 1;
                final String ONE = Integer.toString(one);
                final String COUNT = Integer.toString(count);

                if (COUNT.equalsIgnoreCase(ONE)){
                    List list1 = para.getListFormat().getList();
                    ListLevel level1 = list1.getListLevels().get(0);
                    level1.setNumberPosition(i);
                    level1.setTabPosition(j);
                    level1.setTrailingCharacter(ListTrailingCharacter.TAB);
                    i=i+10;
                    j=j+10;
                }
                else{
                    List list0 = para.getListFormat().getList();
                    ListLevel level0 = list0.getListLevels().get(0);
                    level0.setNumberPosition(i);
                    level0.setTabPosition(j);
                    level0.setTrailingCharacter(ListTrailingCharacter.TAB);
                    i=i+10;
                    j=j+10;
                }
            }

        }
        doc.save("D://Test//OutputWord.doc");
    }
}

Atached are my Input ,Output and ExpectedOutputWord file.
How can I customize spacing between bullet and Text???

Thanks
Sonam

Hi Sonam,

Thanks for your inquiry.

*sonam.arora:

But it works only upto three multi levels.As soon as I add one more level and change the code for same as below, the indentation is handle upto list3 only.*

Please modify your code as shown below to get the required output.

ListLevel.NumberPosition Property : Returns or sets the position (in points) of the number or bullet for the list level.
ListLevel.TabPosition Property : Returns or sets the tab position (in points) for the list level.

Document doc = new Document(MyDir + "InputWord-aspose.doc");
List list1 = doc.getLists().get(0);
ListLevel level1 = list1.getListLevels().get(0);
level1.setNumberPosition(18);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list2 = doc.getLists().get(1);
level1 = list2.getListLevels().get(0);
level1.setNumberPosition(72);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list3 = doc.getLists().get(2);
level1 = list3.getListLevels().get(0);
level1.setNumberPosition(108);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list4 = doc.getLists().get(3);
level1 = list4.getListLevels().get(0);
level1.setNumberPosition(108);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
List list5 = doc.getLists().get(4);
level1 = list5.getListLevels().get(0);
level1.setNumberPosition(180);
level1.setTabPosition(72);
level1.setTrailingCharacter(ListTrailingCharacter.TAB);
doc.updateListLabels();
doc.save(MyDir + "Out.docx"); 

*sonam.arora:

Is there any method throgh which i can get that whether the next paragraph has different bullet than the previos one.*

You may use Node.NextSibling property to get the node immediately following this node. If this node is Paragraph node, you can check either it is list item or not. You may use Node.NextPreOrder method to get the next node according to the pre-order tree traversal algorithm.

*sonam.arora:
H
However facing below error as I try to get 10th index.
that when below code is added.:
List list11 = doc.getLists().get(10);
level1 = list11.getListLevels().get(0);
level1.setNumberPosition(64);
level1.setTabPosition(90);

Is aspose able to handle indents only upto 9 levels?*

A list in a Microsoft Word document is a set of list formatting properties. Each list can have up to 9 levels and formatting properties, such as number style, start value, indent, tab position etc are defined separately for each level.

I am working over your last query and will update you asap.

Hi Sonam,

Thanks for your patience.

*sonam.arora:

Atached are my Input ,Output and ExpectedOutputWord file.
How can I customize spacing between bullet and Text???*

I have modified your code, please check the following highlighted code below. In your case, I suggest you please use the TabPosition, TextPosition and NumberPosition to get the required output.

Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "InputWord.docx");
int count = 0;
int i = 0;
int j = 8;
for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.isListItem())
    {
        count++;
        final int one = 1;
        final String ONE = Integer.toString(one);
        final String COUNT = Integer.toString(count);
        // para.getParagraphFormat().setLeftIndent(0.0);
        if (COUNT.equalsIgnoreCase(ONE))
        {
            List list1 = para.getListFormat().getList();
            ListLevel level1 = list1.getListLevels().get(0);
            level1.setNumberPosition(i);
            level1.setTextPosition(i);
            level1.setTabPosition(j);
            level1.setTrailingCharacter(ListTrailingCharacter.TAB);
            i = i + 10;
            j = j + 10;
        }
        else
        {
            List list0 = para.getListFormat().getList();
            ListLevel level0 = list0.getListLevels().get(0);
            level0.setNumberPosition(i);
            level0.setTextPosition(i);
            level0.setTabPosition(j);
            level0.setTrailingCharacter(ListTrailingCharacter.TAB);
            i = i + 10;
            // j=j+10;
        }
    }
}
doc.save(MyDir + "Out.docx");

Hi Tahir,

Appreciate your reply.

One more added query I have.
Please suggest how can I use Node.NextSibling method in my code in last query.As in my code I am taking each list as a paragraph and checking whether this is list item.Please suggest below is the code for reference.

public class Sample {
    public static void main(String[] args) throws Exception {
        Document doc = new Document("InputWord.doc");
        int count = 0;
        int i = 0;
        int j = 8;
        for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH , true))
        {
            if(para.isListItem())
            {
                count++;
                final int one = 1;
                final String ONE = Integer.toString(one);
                final String COUNT = Integer.toString(count);

                if (COUNT.equalsIgnoreCase(ONE)){
                    List list1 = para.getListFormat().getList();
                    ListLevel level1 = list1.getListLevels().get(0);
                    level1.setNumberPosition(i);
                    level1.setTabPosition(j);
                    level1.setTrailingCharacter(ListTrailingCharacter.TAB);
                    i=i+10;
                    j=j+10;
                }
                else{
                    List list0 = para.getListFormat().getList();
                    ListLevel level0 = list0.getListLevels().get(0);
                    level0.setNumberPosition(i);
                    level0.setTabPosition(j);.
                    level0.setTrailingCharacter(ListTrailingCharacter.TAB);
                    i=i+10;
                    j=j+10;
                }
            }

        }
        doc.save("OutputWord.doc");
    }
}

Thanks

Regards
Sonam

Hey Tahir,

Thanks for your quick reply.But the code provided seems not working.
In my Input file there is a space between bullet and Text .and when Output doc is created the space is still there.

Attached are the Input and Output files for reference.

Regards
Sonam Arora

Hi Sonam,

Thanks for your inquiry.

*sonam.arora:

Please suggest how can I use Node.NextSibling method in my code in last query.As in my code I am taking each list as a paragraph and checking whether this is list item.Please suggest below is the code for reference.*

In your case, you do not need to use Node.NextSibling. You can get the next paragraph from paragraph collection. See Document.getChildNodes method.

*sonam.arora:

But the code provided seems not working.
In my Input file there is a space between bullet and Text .and when Output doc is created the space is still there.*

The shared code works fine. There are spaces between bullet and text. Please check the attached image for detail. You can find the empty Run nodes from the list and remove them. See the highlighted code snippet.

Document doc = new Document(MyDir + "Input+-+28May.doc");
int count = 0;
int i = 0;
int j = 8;
for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.isListItem())
    {
        for (Run run : (Iterable)doc.getChildNodes(NodeType.RUN, true))
        {
            if (run.toString(SaveFormat.TEXT).trim().isEmpty())
            {
                run.remove();
            }
        }
        count++;
        final int one = 1;
        final String ONE = Integer.toString(one);
        final String COUNT = Integer.toString(count);
        if (COUNT.equalsIgnoreCase(ONE))
        {
            List list1 = para.getListFormat().getList();
            ListLevel level1 = list1.getListLevels().get(0);
            level1.setNumberPosition(i);
            level1.setTextPosition(i);
            level1.setTabPosition(j);
            level1.setTrailingCharacter(ListTrailingCharacter.TAB);
            i = i + 10;
            j = j + 10;
        }
        else
        {
            List list0 = para.getListFormat().getList();
            ListLevel level0 = list0.getListLevels().get(0);
            level0.setNumberPosition(i);
            level0.setTextPosition(i);
            level0.setTabPosition(j);
            level0.setTrailingCharacter(ListTrailingCharacter.TAB);
            i = i + 10;
        }
    }
}
doc.save(MyDir + "Out.docx");

Hi Tahir,

For your code provided above.we are using Aspose.Words 14.2 and have upgraded to 14.4.0 version.

There’s error @ TEXT word : if(run.toString(SaveFormat.TEXT).trim().isEmpty())

As I use,

if(run.toString(SaveFormat.TeX).trim().isEmpty())

I got the excpetion like:

Exception in thread "main" java.lang.IllegalStateException: Exporting fragments of a document in this format is not supported.
at com.aspose.words.ëD2.ëLV(Unknown Source)
at com.aspose.words.Node.toString(Unknown Source)

Waiting for soon reply.

Thanks
Sonam Arora

Hi Sonam,

Please accept my apologies for your inconvenience.

Please use Paragraph.getRuns method as shown in following highlighted code snippet to fix this issue. Please let us know if you still face any issue.

Document doc = new Document(MyDir + "Input+-+28May.doc");
int count = 0;
int i = 0;
int j = 8;
for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.isListItem())
    {
        for (Run run : para.getRuns())
        {
            if (run.toString(SaveFormat.TEXT).trim().isEmpty())
            {
                run.remove();
            }
        }
        count++;
        final int one = 1;
        final String ONE = Integer.toString(one);
        final String COUNT = Integer.toString(count);
        if (COUNT.equalsIgnoreCase(ONE))
        {
            List list1 = para.getListFormat().getList();
            ListLevel level1 = list1.getListLevels().get(0);
            level1.setNumberPosition(i);
            level1.setTextPosition(i);
            level1.setTabPosition(j);
            level1.setTrailingCharacter(ListTrailingCharacter.TAB);
            i = i + 10;
            j = j + 10;
        }
        else
        {
            List list0 = para.getListFormat().getList();
            ListLevel level0 = list0.getListLevels().get(0);
            level0.setNumberPosition(i);
            level0.setTextPosition(i);
            level0.setTabPosition(j);
            level0.setTrailingCharacter(ListTrailingCharacter.TAB);
            i = i + 10;
        }
    }
}
doc.save(MyDir + "Out.docx");

Hi Tahir,

Thats fine.

Well I have tried the above code.and I am facing the same issue @ line if(run.toString(SaveFormat.TEXT).trim().isEmpty()) — TEXT NOT AVAILABLE

Please assist.

Thanks
Sonam