Skip to content

Commit d2bf0ac

Browse files
Merge pull request #311 from SyncfusionExamples/ES-904866-How-to-apply-bold-formatting-to-the-content-between-placeholders
Apply-bold-formatting-to-the-content-between-placeholders
2 parents dfa6e4e + 0458338 commit d2bf0ac

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-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.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apply-bold-between-placeholder", "Apply-bold-between-placeholder\Apply-bold-between-placeholder.csproj", "{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}"
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+
{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.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 = {2127D849-B1E6-4BC4-8628-B8D6F0142835}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 24 additions & 0 deletions
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>Apply_bold_between_placeholder</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.NET" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using System.Text.RegularExpressions;
4+
5+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
6+
{
7+
// Create a WordDocument instance by loading the DOCX file from the file stream.
8+
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Docx))
9+
{
10+
// Apply bold formatting to specific text using a regular expression.
11+
ApplyBoldFormatting(document);
12+
13+
// Save the modified document to an output file.
14+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
15+
{
16+
// Save the modified Word document to the specified file path.
17+
document.Save(outputFileStream, FormatType.Docx);
18+
}
19+
}
20+
}
21+
22+
/// <summary>
23+
/// Applies bold formatting to text enclosed in <b>...</b> tags in the Word document.
24+
/// </summary>
25+
static void ApplyBoldFormatting(WordDocument document)
26+
{
27+
// Define a regular expression to find all occurrences of <b>...</b>.
28+
Regex regex = new Regex("<b>(.*?)</b>");
29+
30+
// Find all matches of the regex pattern in the document.
31+
TextSelection[] matches = document.FindAll(regex);
32+
33+
// Iterate through each match found in the document.
34+
foreach (TextSelection match in matches)
35+
{
36+
// Get the entire text range of the matched content.
37+
WTextRange textRange = match.GetAsOneRange();
38+
39+
// Extract the full text of the match.
40+
string fullText = textRange.Text;
41+
42+
// Define the length of the opening tag <b>.
43+
int startTagLength = "<b>".Length;
44+
45+
// Find the index of the closing tag </b>.
46+
int endTagIndex = fullText.LastIndexOf("</b>");
47+
48+
// Extract the opening tag, bold text, and closing tag as separate strings.
49+
string startTag = fullText.Substring(0, startTagLength);
50+
string boldText = fullText.Substring(startTagLength, endTagIndex - startTagLength);
51+
string endTag = fullText.Substring(endTagIndex);
52+
53+
// Create new text ranges for each part (opening tag, bold text, and closing tag).
54+
WTextRange startTextRange = CreateTextRange(textRange, startTag);
55+
WTextRange boldTextRange = CreateTextRange(textRange, boldText);
56+
WTextRange endTextRange = CreateTextRange(textRange, endTag);
57+
58+
// Apply bold formatting to the text range containing the bold text.
59+
boldTextRange.CharacterFormat.Bold = true;
60+
61+
// Replace the original text range with the newly created text ranges in the paragraph.
62+
WParagraph paragraph = textRange.OwnerParagraph;
63+
64+
// Get the index of the original text range within the paragraph.
65+
int index = paragraph.ChildEntities.IndexOf(textRange);
66+
67+
// Remove the original text range from the paragraph.
68+
paragraph.ChildEntities.RemoveAt(index);
69+
70+
// Insert the new text ranges (in order: closing tag, bold text, opening tag) into the paragraph.
71+
paragraph.ChildEntities.Insert(index, endTextRange);
72+
paragraph.ChildEntities.Insert(index, boldTextRange);
73+
paragraph.ChildEntities.Insert(index, startTextRange);
74+
}
75+
}
76+
77+
/// <summary>
78+
/// Creates a new text range with the specified text, copying formatting from the original range.
79+
/// </summary>
80+
static WTextRange CreateTextRange(WTextRange original, string text)
81+
{
82+
// Clone the original text range to preserve its formatting.
83+
WTextRange newTextRange = original.Clone() as WTextRange;
84+
85+
// Set the text for the new text range.
86+
newTextRange.Text = text;
87+
88+
return newTextRange;
89+
}

0 commit comments

Comments
 (0)