Skip to content

Commit 10591f5

Browse files
901973-Added find and apply bold to replaced content sample
1 parent 99a31d7 commit 10591f5

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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("Adventure Works Cycles"));
19+
20+
// Get the selected text as a single text range.
21+
WTextRange textRange = selection.GetAsOneRange();
22+
23+
// Get the text body and paragraph of selected text.
24+
WTextBody body = textRange.OwnerParagraph.OwnerTextBody;
25+
WParagraph paragraph = textRange.OwnerParagraph;
26+
27+
// Get the index of the paragraph within the text body.
28+
int paraIndex = body.ChildEntities.IndexOf(paragraph);
29+
30+
// Replace the selected text with the replace content.
31+
document.Replace("Adventure Works Cycles", "Company name", true, true);
32+
33+
// Iterate the body from the replaced paragraph.
34+
for (int i = paraIndex; i < body.Count; i++)
35+
{
36+
// Access each paragraph in the body.
37+
WParagraph para = body.ChildEntities[i] as WParagraph;
38+
39+
// Search for the word within the current paragraph.
40+
TextSelection replacedSelection = para.Find("Company name", true, false);
41+
42+
if (replacedSelection != null)
43+
{
44+
// Get the selected text as a single text range.
45+
WTextRange replacedTextRange = replacedSelection.GetAsOneRange();
46+
47+
// Apply bold formatting to the text range.
48+
replacedTextRange.CharacterFormat.Bold = true;
49+
break;
50+
}
51+
}
52+
53+
// Save the modified document to the specified output path.
54+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
55+
{
56+
document.Save(outputStream, FormatType.Docx);
57+
}
58+
}
59+
}
60+
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)