Removing Style property for a Run

Hello,

How can we remove specific properties on a Run. For example we have a run as:

<w:r>
	<w:rPr>
		<w:rStyle w:val="DefaultParagraphFont"/>
	</w:rPr>
	<w:t>Yonex</w:t>
</w:r>

I would like to remove the Style property completely. So the run is like:

<w:r>
	<w:t>Yonex</w:t>
</w:r>

One approach is to clone the run and set value using run.text = "Some value". But this may not work always, because in case if the the document has track changes enabled, this change will be treated as a redline. Is there a cleaner way ?

Thanks !

@kml2020 You can use Font.clearFormatting() method to achieve this:

Document doc = new Document("C:\\Temp\\in.docx");
Iterable<Run> runs  = doc.getChildNodes(NodeType.RUN, true);
for (Run r : runs) {
    r.getFont().clearFormatting();
}
doc.save("C:\\Temp\\out.docx");

But please note, using Aspose.Words you cannot work with XML representation of the document directly. When you open the document using Aspose.Words it is read into the DOM, and further changes are made in the DOM, not in the XML.

1 Like

Your suggestion works great, @alexey.noskov. Thank you for the response!

1 Like