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

Free Support Forum - aspose.com

How to update paragaph style using Java

Hello, I want to update a Document with different styles based on some values.
If the paragraph just contains for example “A”, it should have a heading 1, but the next paragraphs “Abstract”, “Aspose” etc should have a body text style.

At the moment all paragraphs seem to remain as the default body text style…

I’ve also tried updating the style of the run, which resulted in all paragraphs being heading 1.

This is the code I’m currently using, could you let me know what’s causing errors?

public static void Insert(List listPara, String docName) throws Exception {
Document doc = new Document();
Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph();
for (String paragraphText : listPara) {
if (paragraphText.length() == 2){
Run run = new Run(doc, paragraphText);
paragraph.appendChild(run);
paragraph.getParagraphFormat().setStyleIdentifier(1); // HEADING 1 Style Identifier
doc.updateFields();
doc.save(docName);
} else {
Run run = new Run(doc, paragraphText);
paragraph.appendChild(run);
paragraph.getParagraphFormat().setStyleIdentifier(66); // Body Text Style Identifier
doc.updateFields();
doc.save(docName);
}

    }

@daviep123

Please call Run.Font.ClearFormatting method to clear the font formatting of Run node and Paragraph.ParagraphFormat.ClearFormatting method for paragraph Node. After calling these methods, please set the style for paragraph.

If you still face problem, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.