WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 7c29bb8

Browse files
Merge pull request #489 from SyncfusionExamples/963171-Add-Retrieve-and-replace-superscript-subscript-text
963171 - Add DocIO Sample for Retrieve and replace superscript subscript text.
2 parents 8b9c552 + cd29948 commit 7c29bb8

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Retrieve-and-replace-superscript-subscript-text/Retrieve-and-replace-superscript-subscript-text.csproj" />
3+
</Solution>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Retrieve_and_replace_superscript_subscript_text</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Input.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>

0 commit comments

Comments
 (0)