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 000000000..932d40a25 --- /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 000000000..474573952 --- /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 000000000..21412cc16 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 000000000..5f282702b --- /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 000000000..ab48b360f --- /dev/null +++ b/Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Program.cs @@ -0,0 +1,89 @@ +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. + ApplyBoldFormatting(document); + + // Save the modified document to an output file. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + // Save the modified Word document to the specified file path. + document.Save(outputFileStream, FormatType.Docx); + } + } +} + +/// +/// Applies bold formatting to text enclosed in ... tags in the Word document. +/// +static void ApplyBoldFormatting(WordDocument document) +{ + // Define a regular expression to find all occurrences of .... + Regex regex = new Regex("(.*?)"); + + // Find all matches of the regex pattern in the document. + TextSelection[] matches = document.FindAll(regex); + + // Iterate through each match found in the document. + foreach (TextSelection match in matches) + { + // Get the entire text range of the matched content. + WTextRange textRange = match.GetAsOneRange(); + + // Extract the full text of the match. + string fullText = textRange.Text; + + // Define the length of the opening tag . + int startTagLength = "".Length; + + // Find the index of the closing tag . + int endTagIndex = fullText.LastIndexOf(""); + + // Extract the opening tag, bold text, and closing tag as separate strings. + string startTag = fullText.Substring(0, startTagLength); + string boldText = fullText.Substring(startTagLength, endTagIndex - startTagLength); + string endTag = fullText.Substring(endTagIndex); + + // Create new text ranges for each part (opening tag, bold text, and closing tag). + WTextRange startTextRange = CreateTextRange(textRange, startTag); + WTextRange boldTextRange = CreateTextRange(textRange, boldText); + WTextRange endTextRange = CreateTextRange(textRange, endTag); + + // Apply bold formatting to the text range containing the bold text. + boldTextRange.CharacterFormat.Bold = true; + + // Replace the original text range with the newly created text ranges in the paragraph. + WParagraph paragraph = textRange.OwnerParagraph; + + // Get the index of the original text range within the paragraph. + int index = paragraph.ChildEntities.IndexOf(textRange); + + // Remove the original text range from the paragraph. + paragraph.ChildEntities.RemoveAt(index); + + // Insert the new text ranges (in order: closing tag, bold text, opening tag) into the paragraph. + paragraph.ChildEntities.Insert(index, endTextRange); + paragraph.ChildEntities.Insert(index, boldTextRange); + paragraph.ChildEntities.Insert(index, startTextRange); + } +} + +/// +/// Creates a new text range with the specified text, copying formatting from the original range. +/// +static WTextRange CreateTextRange(WTextRange original, string text) +{ + // Clone the original text range to preserve its formatting. + WTextRange newTextRange = original.Clone() as WTextRange; + + // Set the text for the new text range. + newTextRange.Text = text; + + return newTextRange; +}