Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Retrieve-and-replace-superscript-subscript-text/Retrieve-and-replace-superscript-subscript-text.csproj" />
</Solution>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -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();
}
/// <summary>
/// Replaces superscript or subscript text in a Word document.
/// </summary>
/// <param name="document">The Word document to process.</param>
/// <param name="subSuperScriptType">Type of script</param>
/// <param name="displayNormalText">True if the replaced text should be converted to normal text; false to keep formatting.</param>
static void ReplaceSuperscriptAndSubscriptText(WordDocument document, string subSuperScriptType, bool displayNormalText)
{
// Find all text ranges in the document that have superscript or subscript formatting.
List<Entity> 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} </{subSuperScriptType}>";
// If the replaced content displayed as normal text
if(displayNormalText)
{
// Set SubSuperScript as none.
textRange.CharacterFormat.SubSuperScript = SubSuperScript.None;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Retrieve_and_replace_superscript_subscript_text</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Input.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading