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.
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.
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");
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.
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?
We are using Aspose 15.10, that had the issue.
This works fine with Aspose 16.6, as expected.
Thanks.