diff --git a/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder.sln b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder.sln new file mode 100644 index 00000000..932d40a2 --- /dev/null +++ b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35309.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apply-bold-between-placeholder", "Apply-bold-between-placeholder\Apply-bold-between-placeholder.csproj", "{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2127D849-B1E6-4BC4-8628-B8D6F0142835} + EndGlobalSection +EndGlobal diff --git a/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Apply-bold-between-placeholder.csproj b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Apply-bold-between-placeholder.csproj new file mode 100644 index 00000000..47457395 --- /dev/null +++ b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Apply-bold-between-placeholder.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Apply_bold_between_placeholder + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Data/Template.docx b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Data/Template.docx new file mode 100644 index 00000000..311607ed Binary files /dev/null and b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Data/Template.docx differ diff --git a/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Output/.gitkeep b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Output/.gitkeep new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Program.cs b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Program.cs new file mode 100644 index 00000000..c6bcd329 --- /dev/null +++ b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Program.cs @@ -0,0 +1,129 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIO; +using System.Text.RegularExpressions; + +using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read)) +{ + // Create a WordDocument instance by loading the DOCX file from the file stream. + using (WordDocument document = new WordDocument(inputFileStream, FormatType.Docx)) + { + // Apply bold formatting to specific text using a regular expression. + ApplyBoldUsingRegex(document); + + // Save the modified document to an output file. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + document.Save(outputFileStream, FormatType.Docx); + } + } +} + +// Method to apply bold formatting to text matching a specific regex pattern. +static void ApplyBoldUsingRegex(WordDocument document) +{ + int startTagIndex = 0; // To store the index of the start tag. + int endTagIndex = 0; // To store the index of the end tag. + WParagraph para = null; // Reference to the paragraph containing the tags. + WTextRange startTextRange = null; // Text range for the start tag. + WTextRange endTextRange = null; // Text range for the end tag. + + // Find the text that matches the ... pattern using a regex. + TextSelection[] textSelections = document.FindAll(new Regex("(.*)")); + + foreach (TextSelection textSelection in textSelections) + { + // Get all text ranges that match the regex pattern. + WTextRange[] textRanges = textSelection.GetRanges(); + + // Iterate through each matched text range. + for (int i = 0; i < textRanges.Length; i++) + { + WTextRange textRange = textRanges[i]; + + // If the text range contains both and tags. + if (i == 0 || i == textRanges.Length - 1) + { + if (textRange.Text.Contains("") && textRange.Text.Contains("")) + { + // Process the text with both start and end tags. + ProcessTextWithStartAndEndTags(textRange); + } + else if (textRange.Text.Contains("") || textRange.Text.Contains("")) + { + // Process the text with only the start or end tag. + ProcessTextWithPartialTags(textRange, ref para, ref startTextRange, ref endTextRange, ref startTagIndex, ref endTagIndex); + } + } + else + { + // Apply bold formatting to the text between and . + textRange.CharacterFormat.Bold = true; + } + } + + // Insert the start and end text ranges if applicable. + if (para != null) + { + para.ChildEntities.Insert(startTagIndex, startTextRange); + para.ChildEntities.Insert(endTagIndex + 1, endTextRange); + } + } +} + +// Process text that contains both and tags. +static void ProcessTextWithStartAndEndTags(WTextRange textRange) +{ + // Find the indexes of the start and end tags. + int startIndex = textRange.Text.IndexOf("") + 3; + int endIndex = textRange.Text.IndexOf(""); + + // Create text ranges for the text before the start tag and after the end tag. + WTextRange startTextRange1 = CreateTextRange(textRange, textRange.Text.Substring(0, startIndex)); + WTextRange endTextRange1 = CreateTextRange(textRange, textRange.Text.Substring(endIndex)); + + // Extract and format the text within the tags. + string boldText = textRange.Text.Substring(startIndex, endIndex - startIndex); + textRange.Text = boldText; + textRange.CharacterFormat.Bold = true; + + // Insert the start and end text ranges into the paragraph. + WParagraph para = textRange.OwnerParagraph; + int index = para.ChildEntities.IndexOf(textRange); + para.ChildEntities.Insert(index, startTextRange1); + para.ChildEntities.Insert(index + 2, endTextRange1); +} + +// Process text that contains only or only tags. +static void ProcessTextWithPartialTags(WTextRange textRange, ref WParagraph para, ref WTextRange startTextRange, ref WTextRange endTextRange, ref int startTagIndex, ref int endTagIndex) +{ + if (textRange.Text.Contains("")) + { + // If the text contains only the start tag . + int startIndex = textRange.Text.IndexOf("") + 3; + startTextRange = CreateTextRange(textRange, textRange.Text.Substring(0, startIndex)); + para = textRange.OwnerParagraph; + startTagIndex = para.ChildEntities.IndexOf(textRange); + textRange.Text = textRange.Text.Replace("", ""); + } + else if (textRange.Text.Contains("")) + { + // If the text contains only the end tag . + int endIndex = textRange.Text.IndexOf(""); + endTextRange = CreateTextRange(textRange, textRange.Text.Substring(endIndex)); + para = textRange.OwnerParagraph; + endTagIndex = para.ChildEntities.IndexOf(textRange); + textRange.Text = textRange.Text.Replace("", ""); + } + + // Apply bold formatting to the text. + textRange.CharacterFormat.Bold = true; +} + +// Utility method to create a new WTextRange with the given text. +static WTextRange CreateTextRange(WTextRange original, string text) +{ + // Clone the original text range and update its text. + WTextRange newTextRange = original.Clone() as WTextRange; + newTextRange.Text = text; + return newTextRange; +} \ No newline at end of file