|
| 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