Why I cant create my style properly?

Hello, here me again :>>>>
there is a weird thing :
my code is

StyleCollection styles = formattingProcessShareVar.getStudetDocument().getStyles();
Style customStyle = styles.add(StyleType.PARAGRAPH, "custom1");
StyleUtils.merge(customStyle, styleConfigDto.getText());

but when i use formattingProcessShareVar.getStudetDocument().getStyles().get("custom1").getParagraphFormat().getStyleName(), the outcome is ‘Normal’, why ? I thought i should be ‘custom1’ , because style custom1 has some attributes that I want, but I can only get normal which doesn’t have

It seems that you are encountering an issue where the custom style you created is not being recognized as expected. When you retrieve the style name using formattingProcessShareVar.getStudetDocument().getStyles().get("custom1").getParagraphFormat().getStyleName(), it returns ‘Normal’ instead of ‘custom1’.

This behavior may occur if the custom style is not being applied correctly to the paragraphs in your document. Here are a few things to check and try:

  1. Ensure Style Application: After creating the custom style, make sure you are applying it to the paragraphs where you want it to take effect. You can do this by setting the paragraph format of the document builder to your custom style.

    Here’s an example of how to apply the custom style:

    DocumentBuilder builder = new DocumentBuilder(formattingProcessShareVar.getStudetDocument());
    builder.getParagraphFormat().setStyle(customStyle);
    builder.writeln("Your text here");
    
  2. Check for Automatic Updates: If your custom style is set to automatically update, it may revert to the default ‘Normal’ style if the text does not match the style’s criteria. You can disable this feature by setting style.setAutomaticallyUpdate(false); after creating your style.

  3. Verify Style Creation: Ensure that the style is being created successfully and is not being overridden or removed later in your code. You can check the styles collection to confirm that ‘custom1’ exists.

  4. Debugging: Add some debugging statements to print out the styles in the collection after you create your custom style. This will help you verify that the style is indeed added and has the expected properties.

By following these steps, you should be able to resolve the issue and ensure that your custom style is applied correctly. If the problem persists, consider reviewing the relevant sections of the Aspose documentation on styling to ensure that all necessary steps are being followed.

If you need further assistance, feel free to ask!

Sources:
[1]: Word Document Styling | Aspose.Words Java Document Processing API
[2]: ExStyles.java Example

let me explain more clearly, style that I create ‘custom1.getParagraphFormat().getLeftIndent()’ is 20, but when i use paragraph.getParagraphFormat().setStyle(custom1);, its getParagraphFormat().getLeftIndent() is still 0, why ? is there something I missing ?

@Madecho The behavior is expected. When you create a custom style, it does not have any formatting applied. If you create the style and save the document, you can see the following style definition:

<w:style w:type="paragraph" w:customStyle="1" w:styleId="custom1">
   <w:name w:val="custom1" />
</w:style>

As you can the style defines only custom style name. In this case MS Word, as well as Aspose.Words, uses Normal style as a base style for the custom style. So the custom style’s paragraph format is inherited from normal style.
If you modify paragraph format like this:

Document doc = new Document();
StyleCollection styles = doc.getStyles();
Style customStyle = styles.add(StyleType.PARAGRAPH, "custom1");
customStyle.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
doc.save("C:\\Temp\\out.docx");

the style definition will look like the following:

<w:style w:type="paragraph" w:customStyle="1" w:styleId="custom1">
	<w:name w:val="custom1" />
	<w:pPr>
		<w:jc w:val="center" />
	</w:pPr>
</w:style>

As you can see now the paragraph format is defined in the style, but paragraph format does not have style name specified, so it is supposed that paragraph format is based on the normal style.

@alexey.noskov so how can i use the style that I already set. like custom1.getParagraphFormat().getLeftIndent()’ is already 20, how can I apply it to a certain paragraph that I want

@Madecho You can use the following code to apply the style to the paragraph:

Document doc = new Document();
StyleCollection styles = doc.getStyles();
Style customStyle = styles.add(StyleType.PARAGRAPH, "custom1");
customStyle.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

// Apply the style for the paragraph
Paragraph p = doc.getFirstSection().getBody().getFirstParagraph();
p.getParagraphFormat().setStyle(customStyle);

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

@alexey.noskov Hello Dearrrrr alexey, here is how I debugged.


as the picture shows, custom1.getParagraphFormat().getLeftIndent() is 20
however , after I run paragraph.getParagraphFormat().setStyle(custom1);

the leftIndent is still 0, that’s really confusing. I think I have done something wrong, but I don’t know where I did it

@Madecho Most likely indent is set explicitly in your paragraph. Explicitly set value overrides the value specified in the applied style. The following code returns 20, just as expected:

Document doc = new Document();
StyleCollection styles = doc.getStyles();
Style customStyle = styles.add(StyleType.PARAGRAPH, "custom1");
customStyle.getParagraphFormat().setLeftIndent(20);
        
// Apply the style for the paragraph
Paragraph p = doc.getFirstSection().getBody().getFirstParagraph();
p.getParagraphFormat().setStyle(customStyle);
        
System.out.println(p.getParagraphFormat().getLeftIndent()); // Returns 20

Please try modifying your code like this:

// Apply the style for the paragraph
Paragraph p = doc.getFirstSection().getBody().getFirstParagraph();
p.getParagraphFormat().clearFormatting();
p.getParagraphFormat().setStyle(customStyle);

@alexey.noskov Love you !
btw: aspose words is a huge black box!

1 Like