How to apply styles to an existing list using ASPOSE.WORD in java

Last post 04-03-2012, 9:47 AM by awais.hafeez. 12 replies.
Sort Posts: Previous Next
  •  02-09-2012, 11:29 AM 360461

    lock [lo] How to apply styles to an existing list using ASPOSE.WORD in java

    Hi, I want to convert an existing .html file to a WORD doc in .docx format. In the existing html  contains two lists, one multilevel and one single list.

    I am trying to see if i can apply styles for existing list using ASPOSE.WORD java API, so when it export, it looks in WORD document in a certain way.

    For example:
    I want to have multilevel list align left justified, not indented, but keep single list indented. Right now the multilevel list is left justified after converted.

    Can you let me know the following?
    1. how can I grab existing list from Document object?
    2. is there a way I can detect a multilevel list and single list?
    3. how can i apply styles to them?


      I am attaching test.zip here that contains following files

      Test.java - is source code that i am running to convert .html to a Word.docx file
      bodyHtml.html - is source html that i used in this sample to convert to .docx file
      template.dotx - is the word template that i am using for converting, it has predefined multilevel and single level list styles.
      HTML2WordTest21874.DOCX - is resulting word file
     
    The issue right now:
    Multilevel list shows as indented, it ignored predefined list style in the word template. I want it to be left justified.

    Thanks for your help,

    Yao
     
  •  02-10-2012, 12:04 AM 360548 in reply to 360461

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    Hi Yao,

    Thanks for your inquiry.

    First of all, I would suggest you please read the following article on Lists in Aspose.Words:

    1. A list in a Microsoft Word document is a set of list formatting properties. The formatting of the lists is stored in the ListCollection separately from the paragraphs of text. You can use DocumentBase.getLists property to access the list formatting used in the document.

    2. You can detect if a List is multilevel or not by using List.IsMultiLevel property.
    3. Please go through the following article:

    Moreover, I am not able to see any attachments with this thread. Please attach the files again for further investigation.

    Please let us know if you need more information, we are always glad to help you.

    Best Regards,

    Awais Hafeez
    Support Developer
    Aspose Sialkot Team
    Aspose - Your File Format Experts

    Keep in touch! We're on Twitter and Facebook
     
  •  02-10-2012, 8:53 AM 360742 in reply to 360548

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    Attachment: Present (inaccessible)

    Awais Hafeez,

       Thanks for replying. I will take a look at links you provided above. Let me attched zip file here.

    Yao

     
  •  02-10-2012, 9:04 AM 360744 in reply to 360742

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    I tried following code for my problem before, but it didn't work

    //I created the list template, first level I want starts with A. 2nd I want starts with 1. 3rd level wants to start with a. 4th i. and i want first level left adjustified, then indent 0.25 after for each level

    List listTemplate = doc.getLists().add(ListTemplate.NUMBER_UPPERCASE_LETTER_DOT);
              
                int i=0;
                for (ListLevel level : (Iterable<ListLevel>) listTemplate.getListLevels()){
                    level.setNumberPosition(i*0.25);
                    level.setTextPosition(0.25+i*0.25);
                    level.setTabPosition(0.25+i*0.25);
                    i++;
                }

    //Then i tried to set this template to exisitng list

    NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true);
     for (Paragraph para : (Iterable<Paragraph>) paras)
                {
                    if (para.getListFormat().isListItem())
                    {
                       if (log.isDebugEnabled())log.debug("para text="+para.getText());
                                           para.getListFormat().setList(listTemplate);                }
                }

    It didn't work. The list is not indented and number format are messed.

    Thanks,

    Yao

     

     

     
  •  02-14-2012, 1:25 PM 361596 in reply to 360744

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    Hi Yao,

    Thank you for attaching the files here. We're working over your most recent queries and will get back to you soon.

    Best Regards,

    Awais Hafeez
    Support Developer
    Aspose Sialkot Team
    Aspose - Your File Format Experts

    Keep in touch! We're on Twitter and Facebook
     
  •  02-14-2012, 11:47 PM 361682 in reply to 361596

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    Hi there,

    Thanks for your inquiry.

    The List.IsMultiLevel property is a bit misleading as it doesn't actually show if the list in the document is multilevel, rather it defines if the list does have suitable defintions for it to be a multilevel list.

    In order to find the different lists in a document you will need to iterate through the paragraphs and check the ListLevelNumber property. Please see the code below for an example. I apologise as this code is in C# and not Java. If there are any troubles porting the code, please ask for help.

    ArrayList singleListIds = new ArrayList();

    ArrayList multiListIds = new ArrayList();

     

    foreach(Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))

    {

        if (para.IsListItem)

        {

            ListFormat listFormat = para.ListFormat;

            int listId = listFormat.List.ListId;

     

            if (listFormat.ListLevelNumber > 0)

            {

                if (!multiListIds.Contains(listId))

                {

                    multiListIds.Add(listId);

     

                    if (singleListIds.Contains(listId))

                        singleListIds.Remove(listId);

                }

            }

            else

            {

                if (!singleListIds.Contains(listId) && !multiListIds.Contains(listId))

                {

                    singleListIds.Add(listId);

                }

            }

        }

    }

     

    // Set formatting for multi level lists.

    foreach (int listId in multiListIds)

    {

        List list = doc.Lists.GetListByListId(listId);

        foreach (ListLevel level in list.ListLevels)

        {

            level.Font.Color = Color.Red;

        }

    }

     

    // Set formatting for single level lists.

    foreach (int listId in singleListIds)

    {

        List list = doc.Lists.GetListByListId(listId);

        foreach (ListLevel level in list.ListLevels)

        {

            level.Font.Color = Color.Green;

        }

    }


    Thanks,

    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  03-06-2012, 10:31 AM 366201 in reply to 361682

    Re: How to apply styles to an existing list using ASPOSE.WORD in java Java

    I was able to use above code to separate mulitlevel list and signle list item. When i use following code to format each level of multilevel list item, it somehow not apply to all the nodes.

    private static void formatList(int listId, String type, Document doc){
        if (log.isDebugEnabled())log.debug("getting listId="+listId+" type="+type);
         ListCollection listcollection=doc.getLists();
         if (listcollection!=null){
             List list = listcollection.getListByListId(listId);
             if (list!=null){
                 if (log.isDebugEnabled())log.debug("applying format for listId="+listId);
                 int i=0;
                 for (ListLevel level : list.getListLevels()){
                    if (log.isDebugEnabled())log.debug("in formatting list, level is="+i);
                    if (log.isDebugEnabled())log.debug("level.getNumberStyle()="+level.getNumberStyle());
                    if ("m".equalsIgnoreCase(type)){
                        applyNumberStyle(i,level);
                        level.setNumberPosition(i*18);
                        level.setTextPosition(18+i*18);
                        level.setTabPosition(18+i*18);
                        if (log.isDebugEnabled()){
                            log.debug("numberPosition="+level.getNumberPosition());
                            log.debug("textPosition="+level.getTextPosition());
                            log.debug("tabPosition="+level.getTabPosition());
                        }
                    }               
                  i++;
             }//end for
          }
        }
    }

    Sample mulitlevel list:

    1. TestAA
      1. Test11
      2. Test22
        1. Testaa1
          1. Testii1
            1. testaa2
    2. TestBB
    3. TestCC

     

    The resulting formatting, the style apply to node TestAA and it's sub node, but not node TestBB and TestCC which are part of mulitlevel list item, any idea why?

    A.    TestAA

    1.     Test11

    2.     Test22

                                         a.        Testaa1

    i.      Testii1

    a.     testaa2

    B.    TestBB

    C.    TestCC

     

    Filed under: Aspose.Word;Java
     
  •  03-06-2012, 10:38 AM 366206 in reply to 366201

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    By the way, I can see in my log, that each of those list item in my sample are below to listId 50.

    here are log information:

    [2012-03-06 09:56:37,658][DEBUG][com.bofa.rpp.word.WordUtils] - starting getting all mulitlevel lists to multiListIds variable
    [2012-03-06 09:56:37,659][DEBUG][com.bofa.rpp.word.WordUtils] - para text=TestAA
    [2012-03-06 09:56:37,660][DEBUG][com.bofa.rpp.word.WordUtils] - listId=50 add to singleListIds
    [2012-03-06 09:56:37,660][DEBUG][com.bofa.rpp.word.WordUtils] - para text=Test11
    [2012-03-06 09:56:37,660][DEBUG][com.bofa.rpp.word.WordUtils] - listId=50 add to multListIds
    [2012-03-06 09:56:37,660][DEBUG][com.bofa.rpp.word.WordUtils] - singleListIds contains listId50
    [2012-03-06 09:56:37,661][DEBUG][com.bofa.rpp.word.WordUtils] - singleListIds removed listId50
    [2012-03-06 09:56:37,661][DEBUG][com.bofa.rpp.word.WordUtils] - para text=Test22
    [2012-03-06 09:56:37,661][DEBUG][com.bofa.rpp.word.WordUtils] - para text=Testaa1
    [2012-03-06 09:56:37,661][DEBUG][com.bofa.rpp.word.WordUtils] - para text=Testii1
    [2012-03-06 09:56:37,661][DEBUG][com.bofa.rpp.word.WordUtils] - para text=testaa2
    [2012-03-06 09:56:37,662][DEBUG][com.bofa.rpp.word.WordUtils] - para text=TestBB
    [2012-03-06 09:56:37,662][DEBUG][com.bofa.rpp.word.WordUtils] - para text=TestCC
    [2012-03-06 09:56:37,663][DEBUG][com.bofa.rpp.word.WordUtils] - start formatting mulitilevel lists. multiListIds size=1
    [2012-03-06 09:56:37,664][DEBUG][com.bofa.rpp.word.WordUtils] - getting listId=50 type=m
    [2012-03-06 09:56:37,664][DEBUG][com.bofa.rpp.word.WordUtils] - applying format for listId=50
    [2012-03-06 09:56:37,664][DEBUG][com.bofa.rpp.word.WordUtils] - in formatting list, level is=0
    [2012-03-06 09:56:37,664][DEBUG][com.bofa.rpp.word.WordUtils] - level.getNumberStyle()=0
    [2012-03-06 09:56:37,665][DEBUG][com.bofa.rpp.word.WordUtils] - numberPosition=0.0
    [2012-03-06 09:56:37,665][DEBUG][com.bofa.rpp.word.WordUtils] - textPosition=18.0
    [2012-03-06 09:56:37,665][DEBUG][com.bofa.rpp.word.WordUtils] - tabPosition=18.0
    [2012-03-06 09:56:37,665][DEBUG][com.bofa.rpp.word.WordUtils] - in formatting list, level is=1
    [2012-03-06 09:56:37,665][DEBUG][com.bofa.rpp.word.WordUtils] - level.getNumberStyle()=0
    [2012-03-06 09:56:37,665][DEBUG][com.bofa.rpp.word.WordUtils] - numberPosition=18.0
    [2012-03-06 09:56:37,666][DEBUG][com.bofa.rpp.word.WordUtils] - textPosition=36.0
    [2012-03-06 09:56:37,666][DEBUG][com.bofa.rpp.word.WordUtils] - tabPosition=36.0
    [2012-03-06 09:56:37,666][DEBUG][com.bofa.rpp.word.WordUtils] - in formatting list, level is=2
    [2012-03-06 09:56:37,666][DEBUG][com.bofa.rpp.word.WordUtils] - level.getNumberStyle()=0
    [2012-03-06 09:56:37,666][DEBUG][com.bofa.rpp.word.WordUtils] - numberPosition=36.0
    [2012-03-06 09:56:37,666][DEBUG][com.bofa.rpp.word.WordUtils] - textPosition=54.0
    [2012-03-06 09:56:37,666][DEBUG][com.bofa.rpp.word.WordUtils] - tabPosition=54.0
    [2012-03-06 09:56:37,666][DEBUG][com.bofa.rpp.word.WordUtils] - in formatting list, level is=3
    [2012-03-06 09:56:37,667][DEBUG][com.bofa.rpp.word.WordUtils] - level.getNumberStyle()=0
    [2012-03-06 09:56:37,667][DEBUG][com.bofa.rpp.word.WordUtils] - numberPosition=54.0
    [2012-03-06 09:56:37,667][DEBUG][com.bofa.rpp.word.WordUtils] - textPosition=72.0
    [2012-03-06 09:56:37,667][DEBUG][com.bofa.rpp.word.WordUtils] - tabPosition=72.0
    [2012-03-06 09:56:37,667][DEBUG][com.bofa.rpp.word.WordUtils] - in formatting list, level is=4
    [2012-03-06 09:56:37,667][DEBUG][com.bofa.rpp.word.WordUtils] - level.getNumberStyle()=0
    [2012-03-06 09:56:37,667][DEBUG][com.bofa.rpp.word.WordUtils] - numberPosition=72.0
    [2012-03-06 09:56:37,668][DEBUG][com.bofa.rpp.word.WordUtils] - textPosition=90.0
    [2012-03-06 09:56:37,668][DEBUG][com.bofa.rpp.word.WordUtils] - tabPosition=90.0
    [2012-03-06 09:56:37,668][DEBUG][com.bofa.rpp.word.WordUtils] - in formatting list, level is=5
    [2012-03-06 09:56:37,668][DEBUG][com.bofa.rpp.word.WordUtils] - level.getNumberStyle()=2
    [2012-03-06 09:56:37,668][DEBUG][com.bofa.rpp.word.WordUtils] - numberPosition=90.0
    [2012-03-06 09:56:37,668][DEBUG][com.bofa.rpp.word.WordUtils] - textPosition=108.0
    [2012-03-06 09:56:37,668][DEBUG][com.bofa.rpp.word.WordUtils] - tabPosition=108.0
    [2012-03-06 09:56:37,669][DEBUG][com.bofa.rpp.word.WordUtils] - in formatting list, level is=6
    [2012-03-06 09:56:37,669][DEBUG][com.bofa.rpp.word.WordUtils] - level.getNumberStyle()=0
    [2012-03-06 09:56:37,669][DEBUG][com.bofa.rpp.word.WordUtils] - numberPosition=108.0
    [2012-03-06 09:56:37,669][DEBUG][com.bofa.rpp.word.WordUtils] - textPosition=126.0
    [2012-03-06 09:56:37,669][DEBUG][com.bofa.rpp.word.WordUtils] - tabPosition=126.0
    [2012-03-06 09:56:37,669][DEBUG][com.bofa.rpp.word.WordUtils] - in formatting list, level is=7
    [2012-03-06 09:56:37,669][DEBUG][com.bofa.rpp.word.WordUtils] - level.getNumberStyle()=4
    [2012-03-06 09:56:37,670][DEBUG][com.bofa.rpp.word.WordUtils] - numberPosition=126.0
    [2012-03-06 09:56:37,670][DEBUG][com.bofa.rpp.word.WordUtils] - textPosition=144.0
    [2012-03-06 09:56:37,670][DEBUG][com.bofa.rpp.word.WordUtils] - tabPosition=144.0
    [2012-03-06 09:56:37,670][DEBUG][com.bofa.rpp.word.WordUtils] - in formatting list, level is=8
    [2012-03-06 09:56:37,670][DEBUG][com.bofa.rpp.word.WordUtils] - level.getNumberStyle()=2
    [2012-03-06 09:56:37,670][DEBUG][com.bofa.rpp.word.WordUtils] - numberPosition=144.0
    [2012-03-06 09:56:37,670][DEBUG][com.bofa.rpp.word.WordUtils] - textPosition=162.0
    [2012-03-06 09:56:37,671][DEBUG][com.bofa.rpp.word.WordUtils] - tabPosition=162.0

     
  •  03-07-2012, 5:23 AM 366402 in reply to 366206

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    Hi there,

    Thanks for your inquiry

    I suppose this is because those two nodes are a part of the same list level as the first list item. Also perhaps the indent is too large and it is ignored.

    Thanks,

    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  03-07-2012, 11:46 AM 366486 in reply to 366402

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    Thanks for replying. I would think the style should apply to all nodes that belong to the same level, not just 1st item in each level right?

    Also what do you mean by indent is too large it ignored? In my orignal word template, i have it set to increase indent 0.25 inch for each level, with 1st level start at 0. I did some reseach on it, for 0.25inch, it translates to 18points, but the numberPosition seems not coming out right. What I am trying to achieve here is when i export a document that cotains the list, the look and feel should be exactly or very close to my orignal list that's on word document. But that's not what I got here.

    Yao

     

     
  •  03-09-2012, 4:53 AM 367065 in reply to 366486

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    Attachment: Present (inaccessible)
    Hi Yao,

    Thanks for your inquiry. I am afraid, I was unable to reproduce this problem on my side. I used the following code with Aspose.Words for Java v11.1.0 and output produced seems to be perfect:
    Document doc = new Document("C:\\temp\\HTML2WordTest21874.docx");
    
    ArrayList<Integer> singleListIds = new ArrayList<Integer>();
    ArrayList<Integer> multiListIds = new ArrayList<Integer>();
    
    for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
        if (para.isListItem()) {
            ListFormat listFormat = para.getListFormat();
            Integer listId = listFormat.getList().getListId();
            if (listFormat.getListLevelNumber() > 0) {
                if (!multiListIds.contains(listId)) {
                    multiListIds.add(listId);
                    if (singleListIds.contains(listId))
                        singleListIds.remove(listId);
                }
    
            } else {
                if (!singleListIds.contains(listId) && !multiListIds.contains(listId)) {
                    singleListIds.add(listId);
                    System.out.println("true");
                }
            }
        }
    }
    
    // Set formatting for multi level lists.
    for (Integer listId : multiListIds) {
        List list = doc.getLists().getListByListId(listId);
        for (ListLevel level : list.getListLevels()) {
            level.getFont().setColor(Color.RED);
        }
    }
    
    // Set formatting for single level lists.
    for (Integer listId : singleListIds) {
        List list = doc.getLists().getListByListId(listId);
        for (ListLevel level : list.getListLevels()) {
            level.getFont().setColor(Color.GREEN);
        }
    }
    
    doc.save("C:\\temp\\Output.docx");
    Moreover, I have attached input/output documents here for your reference.

    Best Regards,

    Awais Hafeez
    Support Developer
    Aspose Sialkot Team
    Aspose - Your File Format Experts

    Keep in touch! We're on Twitter and Facebook
     
  •  04-02-2012, 10:00 AM 372711 in reply to 367065

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    Attachment: Present (inaccessible)

    Awais,

       Thank you very much for replying. I just tried your sample codes, the color settings work for me, but not number and text position settings. Would you mind to take another look and let me know if there is anyway i can fix this?

    I made a bit change to your codes above to try achieve what i want, see details below. I attached out put document here - Text and Number Poistion test.docx

    My findings:

    You can see for 1st level it apply correct number style, and it sets color to red, which is good. But for number position, for A. it worked, align left justified, but for node B. it doesn't do it.

    for 2nd level it apply correct number style and set color to green, which is good . But number poistion is off as well, i want to 2nd level to be indent 0.25" compare to 1st level, but it indents more.

    /**
     *
     * @param i
     * @param level
     */
     private static void applyNumberStyle(int i, ListLevel level){
        switch (i) {
          case 0:
            level.getFont().setColor(Color.red);
            level.setNumberStyle(NumberStyle.UPPERCASE_LETTER);
            level.setNumberPosition(0);
            level.setTextPosition(ConvertUtil.inchToPoint(0.25));
            level.setTrailingCharacter(ListTrailingCharacter.TAB);
            level.setTabPosition(ConvertUtil.inchToPoint(0.25));
            break;
          case 1:
            level.getFont().setColor(Color.green);
            level.setNumberStyle(NumberStyle.ARABIC);
            level.setNumberPosition(ConvertUtil.inchToPoint(0.25));
            level.setTextPosition(ConvertUtil.inchToPoint(0.50));
            level.setTrailingCharacter(ListTrailingCharacter.TAB);
            level.setTabPosition(ConvertUtil.inchToPoint(0.50));
            break;
          case 2:
            level.setNumberStyle(NumberStyle.LOWERCASE_LETTER);
            break;
          case 3:
            level.setNumberStyle(NumberStyle.LOWERCASE_ROMAN);
            break;
          case 4:
            level.setNumberStyle(NumberStyle.LOWERCASE_LETTER);
            break;
          case 5:
            level.setNumberStyle(NumberStyle.LOWERCASE_ROMAN);
            break;
          case 6:
            level.setNumberStyle(NumberStyle.LOWERCASE_LETTER);
            break;
          case 7:
            level.setNumberStyle(NumberStyle.LOWERCASE_ROMAN);
            break;
          case 8:
            level.setNumberStyle(NumberStyle.LOWERCASE_LETTER);
            break;
          default:
            level.setNumberStyle(NumberStyle.UPPERCASE_LETTER);
        }
     }

     

    calling code:

     //getting all multilevel list out
                ArrayList<Integer> singleListIds = new ArrayList<Integer>();
                ArrayList<Integer> multiListIds = new ArrayList<Integer>();

                for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
                    if (para.isListItem()) {
                        ListFormat listFormat = para.getListFormat();
                        Integer listId = listFormat.getList().getListId();
                        if (listFormat.getListLevelNumber() > 0) {
                            if (!multiListIds.contains(listId)) {
                                multiListIds.add(listId);
                                if (singleListIds.contains(listId))
                                    singleListIds.remove(listId);
                            }

                        } else {
                            if (!singleListIds.contains(listId) && !multiListIds.contains(listId)) {
                                singleListIds.add(listId);
                                //System.out.println("true");
                            }
                        }
                    }
                }

                // Set formatting for multi level lists.
                for (Integer listId : multiListIds) {
                    List list = doc.getLists().getListByListId(listId);
                    int i=0;
                    for (ListLevel level : list.getListLevels()) {
                        applyNumberStyle(i,level);
                        i++;
                    }
                }

     

     

    Filed under: Java
     
  •  04-03-2012, 9:47 AM 373039 in reply to 372711

    Re: How to apply styles to an existing list using ASPOSE.WORD in java

    Hi Yao,

    Thanks for your inquiry. I managed to reproduce this issue on my side. I have logged this issue in our bug tracking system as WORDSNET-6145. Your request has also been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

    Moreover, as a temporary work around, I would suggest you please open 'HTML2WordTest21874.docx' file using MS WORD and save as again to DOCX format. Then please try loading this newly saved DOCX document using Aspose.Words for further document processing tasks. I hope, this will help.

    Best Regards,

    Awais Hafeez
    Support Developer
    Aspose Sialkot Team
    Aspose - Your File Format Experts

    Keep in touch! We're on Twitter and Facebook
     
View as RSS news feed in XML