diff --git a/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text.slnx b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text.slnx new file mode 100644 index 000000000..b43208b27 --- /dev/null +++ b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text.slnx @@ -0,0 +1,3 @@ + + + diff --git a/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Data/Input.docx b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Data/Input.docx new file mode 100644 index 000000000..5f59151e7 Binary files /dev/null and b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Data/Input.docx differ diff --git a/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Output/.gitkeep b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Program.cs b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Program.cs new file mode 100644 index 000000000..c46414972 --- /dev/null +++ b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Program.cs @@ -0,0 +1,46 @@ +using Syncfusion.DocIO.DLS; + +namespace Retrieve_and_replace_superscript_subscript_text +{ + class Program + { + public static void Main(string[] args) + { + // Load the existing Word document. + WordDocument document = new WordDocument(Path.GetFullPath(@"Data\Input.docx")); + // Replace all superscript text and maintain as superscript + ReplaceSuperscriptAndSubscriptText(document, "SuperScript", false); + // Replace all subscript text but convert it into normal text. + ReplaceSuperscriptAndSubscriptText(document, "SubScript", true); + // Save the word document + document.Save(Path.GetFullPath("../../../Output/Output.docx")); + // Close the document. + document.Close(); + } + /// + /// Replaces superscript or subscript text in a Word document. + /// + /// The Word document to process. + /// Type of script + /// True if the replaced text should be converted to normal text; false to keep formatting. + static void ReplaceSuperscriptAndSubscriptText(WordDocument document, string subSuperScriptType, bool displayNormalText) + { + // Find all text ranges in the document that have superscript or subscript formatting. + List textRangesWithsubsuperScript = document.FindAllItemsByProperty(EntityType.TextRange, "CharacterFormat.SubSuperScript", subSuperScriptType); + // Replace the super script text + for (int i = 0; i < textRangesWithsubsuperScript.Count; i++) + { + // Cast the entity to a text range. + WTextRange textRange = textRangesWithsubsuperScript[i] as WTextRange; + // Replace the text + textRange.Text = $"<{subSuperScriptType}> {textRange.Text} "; + // If the replaced content displayed as normal text + if(displayNormalText) + { + // Set SubSuperScript as none. + textRange.CharacterFormat.SubSuperScript = SubSuperScript.None; + } + } + } + } +} \ No newline at end of file diff --git a/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Retrieve-and-replace-superscript-subscript-text.csproj b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Retrieve-and-replace-superscript-subscript-text.csproj new file mode 100644 index 000000000..b0b5e3621 --- /dev/null +++ b/Find-item-in-word-document/Retrieve-and-replace-superscript-subscript-text/.NET/Retrieve-and-replace-superscript-subscript-text/Retrieve-and-replace-superscript-subscript-text.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Retrieve_and_replace_superscript_subscript_text + enable + enable + + + + + + + + + Always + + + Always + + + +