Changing the stye type for existing styles

Hi Team,

As part of our current development we want to update certain styles from paragraph styles to list styles dynamically . Is this possible ? We would want to copy all properties from a paragraph style but only update style type to list. Could you please suggest how we can proceed for such a solution with Aspose

@ManasaVTR

Yes, it is possible to dynamically change certain styles from paragraph styles to list styles in Aspose.Words. You can achieve this by copying the properties of the existing paragraph style and then updating the style type to a list style.

Here’s a general approach to do this:

  1. Retrieve the existing paragraph style that you want to convert.
  2. Create a new list style and copy the properties from the paragraph style to the new list style.
  3. Assign the new list style to the paragraphs that were using the original paragraph style.

Here’s a code example in Java that demonstrates this process:

import com.aspose.words.*;

public class ChangeStyleType {
    public static void main(String[] args) throws Exception {
        // Create a new document
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Create a paragraph style
        Style paragraphStyle = doc.getStyles().add(StyleType.PARAGRAPH, "MyParagraphStyle");
        paragraphStyle.getFont().setName("Arial");
        paragraphStyle.getFont().setSize(12);
        builder.getParagraphFormat().setStyle(paragraphStyle);
        builder.writeln("This is a paragraph with MyParagraphStyle.");

        // Now, let's convert this paragraph style to a list style
        List list = doc.getLists().add(ListTemplate.BULLET_DEFAULT);
        Style listStyle = doc.getStyles().add(StyleType.LIST, "MyListStyle");
        listStyle.getListFormat().setList(list);
        
        // Apply the new list style to the paragraph
        for (Paragraph paragraph : doc.getChildNodes(NodeType.PARAGRAPH, true)) {
            if (paragraph.getParagraphFormat().getStyle().getName().equals("MyParagraphStyle")) {
                paragraph.getParagraphFormat().setStyle(listStyle);
            }
        }

        // Save the document
        doc.save("Output.docx");
    }
}

In this example:

  • A new paragraph style named “MyParagraphStyle” is created and applied to a paragraph.
  • A new list style named “MyListStyle” is created, and the properties from the paragraph style are copied to this list style.
  • Finally, the new list style is applied to the paragraphs that were using the original paragraph style.

This way, you can dynamically change the style type from paragraph to list while retaining the original properties.

For more detailed information, you can refer to the Aspose.Words documentation on styles and lists.

@ManasaVTR There is no way to change style type of existing styles. However, if your would like to apply list numbering to the paragraph with the certain style applied, you can simply apply list to this style.

If possible, please provide your sample input and expected output documents. We will check them and provide you more information.

Thanks Alexey.
Our requirement is to update the lists from a document based on customer properties.
For example, I have attached here a document for which we want to update list level styles, indents, numbering format , number style and so on.

In document you can find a list with style Sch 1 Heading and then different styles like Sch 2 Number for level 1, Sch 3 number for level2 and so on applied for each level.
When I try to fetch the list Aspose, it fetches each of these lists as separate list instead of showing the styles being applied to levels.

I have attached the lists from style gallery . Could you please suggest what would be the best way to process this list and update level details and paragraph indents for each level.

fsXMLTest-1004070613 (10).docx (182.4 KB)

@ManasaVTR In your case you should simply modify the list level applied via the corresponding style. For example the following code demonstrates how to change list label’s color in your document:

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

// Get level 1 style.
Style lvl1 = doc.getStyles().get("Level 1 Heading");
// Get level 2 style.
Style lvl2 = doc.getStyles().get("Level 2 Heading");

// Modify numbering format of the list applied via styles.
lvl1.getListFormat().getListLevel().getFont().setColor(Color.RED);
lvl2.getListFormat().getListLevel().getFont().setColor(Color.GREEN);

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

Thanks Alexey . Can you help with how we can set indents for the paragraphs which are mapped to these levels ?

@ManasaVTR You can set paragraph indent using ParagraphFormat.Leftindent property. Please see the following code:

lvl1.getParagraphFormat().setLeftIndent(100);