1+ using Syncfusion . DocIO . DLS ;
2+
3+ namespace Retrieve_and_replace_superscript_subscript_text
4+ {
5+ class Program
6+ {
7+ public static void Main ( string [ ] args )
8+ {
9+ // Load the existing Word document.
10+ WordDocument document = new WordDocument ( Path . GetFullPath ( @"Data\Input.docx" ) ) ;
11+ // Replace all superscript text and maintain as superscript
12+ ReplaceSuperscriptAndSubscriptText ( document , "SuperScript" , false ) ;
13+ // Replace all subscript text but convert it into normal text.
14+ ReplaceSuperscriptAndSubscriptText ( document , "SubScript" , true ) ;
15+ // Save the word document
16+ document . Save ( Path . GetFullPath ( "../../../Output/Output.docx" ) ) ;
17+ // Close the document.
18+ document . Close ( ) ;
19+ }
20+ /// <summary>
21+ /// Replaces superscript or subscript text in a Word document.
22+ /// </summary>
23+ /// <param name="document">The Word document to process.</param>
24+ /// <param name="subSuperScriptType">Type of script</param>
25+ /// <param name="displayNormalText">True if the replaced text should be converted to normal text; false to keep formatting.</param>
26+ static void ReplaceSuperscriptAndSubscriptText ( WordDocument document , string subSuperScriptType , bool displayNormalText )
27+ {
28+ // Find all text ranges with the given superscript or subscript formatting.
29+ List < Entity > textRangesWithsubsuperScript = document . FindAllItemsByProperty ( EntityType . TextRange , "CharacterFormat.SubSuperScript" , subSuperScriptType ) ;
30+ for ( int i = 0 ; i < textRangesWithsubsuperScript . Count ; i ++ )
31+ {
32+ // Cast the entity to a text range.
33+ WTextRange textRange = textRangesWithsubsuperScript [ i ] as WTextRange ;
34+ // Replace the existing text with new content
35+ textRange . Text = $ "<{ subSuperScriptType } > { textRange . Text } </{ subSuperScriptType } >";
36+ // If the replaced content displayed as normal text
37+ if ( displayNormalText )
38+ {
39+ // Set SubSuperScript as none.
40+ textRange . CharacterFormat . SubSuperScript = SubSuperScript . None ;
41+ }
42+ }
43+ }
44+ }
45+ }
0 commit comments