Skip to content

Commit 3c7919b

Browse files
Merge pull request #256 from DharanitharanA/main
901973-Added find and apply bold to replaced content sample
2 parents 94d409f + d7fcea4 commit 3c7919b

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-and-apply-bold-to-replaced-content", "Find-and-apply-bold-to-replaced-content\Find-and-apply-bold-to-replaced-content.csproj", "{9A62B5BC-B52F-47AD-933E-D1E4BE3C5569}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{9A62B5BC-B52F-47AD-933E-D1E4BE3C5569}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9A62B5BC-B52F-47AD-933E-D1E4BE3C5569}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9A62B5BC-B52F-47AD-933E-D1E4BE3C5569}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9A62B5BC-B52F-47AD-933E-D1E4BE3C5569}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {B238AE7E-59A8-4651-ACC6-E1264E184A2B}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Find_and_apply_bold_to_replaced_content</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+
</Project>

Find-and-Replace/Find-and-apply-bold-to-replaced-content/.NET/Find-and-apply-bold-to-replaced-content/Output/.gitkeep

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using System.Text.RegularExpressions;
4+
5+
namespace Find_and_apply_bold_to_replaced_content
6+
{
7+
internal class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
// Open the input Word document from the specified path.
12+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
13+
{
14+
// Load the Word document into the WordDocument object.
15+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
16+
{
17+
// Find the text pattern using a regular expression.
18+
TextSelection selection = document.Find(new Regex("^<<([^>]*)>>"));
19+
// Get the selected text as a single text range.
20+
WTextRange textRange = selection.GetAsOneRange();
21+
// Get the text body and paragraph of selected text.
22+
WTextBody body = textRange.OwnerParagraph.OwnerTextBody;
23+
WParagraph paragraph = textRange.OwnerParagraph;
24+
// Get the index of the paragraph within the text body.
25+
int paraIndex = body.ChildEntities.IndexOf(paragraph);
26+
// Replace the selected text with the replace content.
27+
document.Replace(selection.SelectedText, "Adventure Works Cycles", true, true);
28+
// Search for the word within the current paragraph.
29+
TextSelection replacedSelection = paragraph.Find("Works", true, false);
30+
if (replacedSelection != null)
31+
{
32+
// Get the selected text as a single text range.
33+
WTextRange replacedTextRange = replacedSelection.GetAsOneRange();
34+
// Apply bold formatting to the text range.
35+
replacedTextRange.CharacterFormat.Bold = true;
36+
}
37+
// Save the modified document to the specified output path.
38+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
39+
{
40+
document.Save(outputStream, FormatType.Docx);
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)