|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +namespace Replace_text_in_list_paragraph_with_HTML |
| 6 | +{ |
| 7 | + class Program |
| 8 | + { |
| 9 | + static void Main(string[] args) |
| 10 | + { |
| 11 | + using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) |
| 12 | + { |
| 13 | + //Open the template Word document. |
| 14 | + using (WordDocument document = new WordDocument(inputFileStream, FormatType.Automatic)) |
| 15 | + { |
| 16 | + string htmlFilePath = @"Data/File.html"; |
| 17 | + //Check if the HTML content is valid. |
| 18 | + if (document.LastSection.Body.IsValidXHTML(htmlFilePath, XHTMLValidationType.None)) |
| 19 | + { |
| 20 | + //Define the variable containing the text to search within the paragraph. |
| 21 | + string variable = "Youth mountain bike"; |
| 22 | + //Find the first occurrence of a particular text in the document |
| 23 | + TextSelection textSelection = document.Find(variable, true, true); |
| 24 | + //Get the found text as single text range |
| 25 | + WTextRange textRange = textSelection.GetAsOneRange(); |
| 26 | + // Get the paragraph containing the found text range |
| 27 | + WParagraph paragraph = textRange.OwnerParagraph; |
| 28 | + //Get the next sibling element of the current paragraph. |
| 29 | + TextBodyItem nextSibling = paragraph.NextSibling as TextBodyItem; |
| 30 | + //Get the index of the current paragraph within its parent text body. |
| 31 | + int sourceIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); |
| 32 | + //Clear all child entities within the paragraph. |
| 33 | + paragraph.ChildEntities.Clear(); |
| 34 | + //Get the list style name from the paragraph. |
| 35 | + string listStyleName = paragraph.ListFormat.CurrentListStyle.Name; |
| 36 | + //Get the current list level number. |
| 37 | + int listLevelNum = paragraph.ListFormat.ListLevelNumber; |
| 38 | + //Append HTML content from the specified file to the paragraph. |
| 39 | + paragraph.AppendHTML(File.ReadAllText(Path.GetFullPath(htmlFilePath))); |
| 40 | + //Determine the index of the next sibling if it exists. |
| 41 | + int nextSiblingIndex = nextSibling != null ? nextSibling.OwnerTextBody.ChildEntities.IndexOf(nextSibling) : -1; |
| 42 | + //Apply the same list style to newly added paragraphs from the HTML content. |
| 43 | + for (int k = sourceIndex; k < paragraph.OwnerTextBody.Count; k++) |
| 44 | + { |
| 45 | + //Stop applying the style if the next sibling is reached. |
| 46 | + if (nextSiblingIndex != -1 && k == nextSiblingIndex) |
| 47 | + { |
| 48 | + break; |
| 49 | + } |
| 50 | + Entity entity = paragraph.OwnerTextBody.ChildEntities[k]; |
| 51 | + //Apply the list style only if the entity is a paragraph. |
| 52 | + if (entity is WParagraph) |
| 53 | + { |
| 54 | + (entity as WParagraph).ListFormat.ApplyStyle(listStyleName); |
| 55 | + (entity as WParagraph).ListFormat.ListLevelNumber = listLevelNum; |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 64 | + { |
| 65 | + //Save the modified Word document to the output file stream. |
| 66 | + document.Save(outputFileStream, FormatType.Docx); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments