Reapplying styles not working as expected

Hi,
I would like to know appropriate way to re-apply styles to paragraphs.

Steps in MS Word:

  1. I have a document, with 4 text-headings marked with “Heading 1” styles.
  2. After applying the style, I have manually formatted few of them (like, chaning alignment to left to right, or changing font color / size etc.)
  3. Now attaching another template, which also has “Heading 1” style defined. While applying, I have selected “Automatically update document styles”
  4. The text-headings are not completely updated to “Heading 1” style, since I have manually applied additional formatting to the text-headings.
    (For example, the alignment, underlining, italics, font coloring are not changed as per the “Heading 1” of the new template).
  5. If I apply “Heading 1” now on the text-headings, then its applied properly

In Aspose:
Till steps 4, its same behavior observed.
But, how to apply the style “Heading 1” on the text-headings?
I tried following code, but it didn’t apply.

private static void reApplyStyle(String fileName) throws Exception {
    Document doc = new Document(fileName);

    doc.setAutomaticallyUpdateSyles(true);
    doc.setAttachedTemplate("d:\Word-Normal-Copy.dotx");

    StyleCollection styles = doc.getStyles();
    String styleName = "Heading 1";
    Style heading1Style = styles.get(styleName);

    ArrayList parasStyle = getParagraphsByStyleName(doc, styleName);
    System.out.println("Paragraphs with style ["+styleName+"] : " + parasStyle.size());
    for (int i = 0; i < parasStyle.size(); i++) {
        Paragraph para = (Paragraph) parasStyle.get(i);
        para.getParagraphFormat().clearFormatting();
        para.getParagraphFormat().setStyle(heading1Style);
    }
    doc.save(fileName);
}

May I know if there is a way to apply the style on text cleanly?

Thanks.

Attaching the “test.docx” used in the above code.
the “Word-Normal-Copy.dotx” is only a copy of Normal.dotx from MS Word, but modified the “Heading 1” style for trying this.

Hi Madhes,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Please share code of getParagraphsByStyleName method
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please share Word-Normal-Copy.dotx

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

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Thanks. Attaching the requested method & word documents.

Hi Madhes,

Thanks for sharing the detail. You applied direct formatting to text in your input document. In this case, you need to remove the direct formatting of text using Run.Font.clearFormatting method. Please use following modified code example to get the required output.

Document doc = new Document(MyDir + "test - input file.docx");
StyleCollection styles = doc.getStyles();
String styleName = "Heading 1";
Style heading1Style = styles.get(styleName);
ArrayList parasStyle = *getParagraphsByStyleName * (doc, styleName);
System.out.println("Paragraphs with style [" + styleName + "] : " + parasStyle.size());
for (int i = 0; i < parasStyle.size(); i++)
{
    Paragraph para = (Paragraph)parasStyle.get(i);
    para.getParagraphFormat().clearFormatting();
    para.getParagraphBreakFont().clearFormatting();
    for (Run run : (Iterable)para.getRuns())
    {
        run.getFont().clearFormatting();
    }
    para.getParagraphFormat().setStyle(heading1Style);
}
doc.setAttachedTemplate(MyDir + "Word-Normal-Copy.dotm");
doc.setAutomaticallyUpdateSyles(true);
doc.save(MyDir + "Out.docx");

Great. Thanks much for the quick update.
It’s applying as expected now.

Hi Madhes,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi,

I observe that, clearing the formatting from run.getFont() as suggested is not working as expected.

Code used:

private static void removeAdditionalFormatting(String fileName, String templateFile, String styleName) throws Exception {
    Document doc = new Document(fileName);
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    Style style = doc.getStyles().get(styleName);

    for (Paragraph paragraph : (Iterable) paragraphs) {
        String currStyle = paragraph.getParagraphFormat().getStyle().getName();
        if (!styleName.equals(currStyle)) {
            // The paragraph’s style is not expected style
            continue;
        }

        // clear style for all runs
        for (Run run : (Iterable) paragraph.getRuns()) {
            run.getFont().clearFormatting();
        }
        paragraph.getParagraphFormat().setStyle(style);
    }

    // When attaching a template to a Word document, ‘setAutomaticallyUpdateStyles’
    // should be set to true, for the styles to get updated from the template.
    doc.setAutomaticallyUpdateSyles(true);
    doc.setAttachedTemplate(templateFile);
    doc.save("OutputAfterClearingAndReapplying.docx");
    System.out.println("Done.");
}

Attaching the following for your review:
source file - MyHeadingDocument.docx
OutputAfterClearingAndReapplying.docx
ExpectedResult.docx

Can you please comment on what is missing? Thanks.

Hi Madhes,

Thanks for your inquiry. Please use ParagraphFormat.clearFormatting method as shown below in your code to get the desired output. We have used the template document shared in this forum thread to test this issue and have not found any issue in output document.

Please let us know if you have any more queries.

String styleName = "MyHeading";
Document doc = new Document(MyDir + "MyHeadingDocument.docx");
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
Style style = doc.getStyles().get(styleName);

for (Paragraph paragraph : (Iterable)paragraphs)
{
    String currStyle = paragraph.getParagraphFormat().getStyle().getName();
    if (!styleName.equals(currStyle))
    {
        // The paragraph's style is not expected style
        continue;
    }

    // clear style for all run
    for (Run run : (Iterable)paragraph.getRuns())
    {
        run.getFont().clearFormatting();
    }
    paragraph.getParagraphFormat().clearFormatting();
    paragraph.getParagraphFormat().setStyle(style);

}

// When attaching a template to a Word document, ‘setAutomaticallyUpdateStyles’
// should be set to true, for the styles to get updated from the template.
doc.setAutomaticallyUpdateSyles(true);

doc.setAttachedTemplate(MyDir + "Word-Normal-Copy.dotm");

doc.save(MyDir + "Out v16.6.0.docx");

Thank you very much. I tried clearing & setting the style separately, but not together. This seem to be solving most of the cases.

However I see an issue, when we modify the style in Word. I will try to reproduce and update. Thanks again.

Hi Madhes,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Thanks. Here is the issue we are facing.

In MS Word:
+ Create a document from a Template (the template has “MyHeading” style, with font color as black, and no underlining).
+ Right click on a custom style “MyHeading” and select “Modify”
+ Add additional formatting options like “Underline”, Font color change to Red.
+ The document now has both the style & content with the modified style

Using the snippet (provided above), I clear the styles, and reapply the same template. After that, I save it as docx & html.
The new docx is having expected results (ie., has the original style, with font color as black, and no underlining)
But, the html is stored with modified style (has font color is still Red, with Underlining).

Attaching Original document (Sample.docx), resultant document (Sample.docx_modified.docx), resultant html (Sample.docx_modified.html).

May I know why this discrepancy?

Hi Madhes,

Thanks for your inquiry. Could you please attach your template document that have “MyHeading” style here for testing? We will investigate the issue on our side and provide you more information.

Thanks. Attaching the template.

Hi Madhes,

Thanks for sharing the template document.

We have tested the scenario using latest version of Aspose.Words for Java 16.6.0 and have not found the share issue. Please use Aspose.Words for Java 16.6.0 and let us know if you have any more queries. We have attached the output html with this post for your kind reference.

We are using Aspose 15.10, that had the issue.
This works fine with Aspose 16.6, as expected.
Thanks.

Hi Madhes,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.