|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +namespace Replace_text_with_HTML_list |
| 5 | +{ |
| 6 | + internal class Program |
| 7 | + { |
| 8 | + // List to store the names of different list styles used in the document. |
| 9 | + static List<string> listStyleNames = new List<string>(); |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + // Load the input Word document from file stream |
| 13 | + using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read)) |
| 14 | + { |
| 15 | + // Open the Word document |
| 16 | + using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) |
| 17 | + { |
| 18 | + // Load the input Word document from file stream |
| 19 | + using (FileStream htmlStream = new FileStream(Path.GetFullPath(@"Data/sample.html"), FileMode.Open, FileAccess.Read)) |
| 20 | + { |
| 21 | + // Open the Word document |
| 22 | + using (WordDocument replaceDoc = new WordDocument(htmlStream, FormatType.Html)) |
| 23 | + { |
| 24 | + //Replace the first word with HTML file content |
| 25 | + ReplaceText(document, "Tag1", replaceDoc, true); |
| 26 | + //Replace the second word with HTML file content |
| 27 | + ReplaceText(document, "Tag2", replaceDoc, false); |
| 28 | + |
| 29 | + // Save the modified document to a new file |
| 30 | + using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write)) |
| 31 | + { |
| 32 | + document.Save(docStream1, FormatType.Docx); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + private static void ReplaceText(WordDocument document, string findText, WordDocument replaceDoc, bool isFirstReplace) |
| 40 | + { |
| 41 | + TextSelection selection = document.Find(findText, true, true); |
| 42 | + if (selection != null) |
| 43 | + { |
| 44 | + //Get the textrange |
| 45 | + WTextRange textRange = selection.GetAsOneRange(); |
| 46 | + //Get the owner paragraph |
| 47 | + WParagraph ownerPara = textRange.OwnerParagraph; |
| 48 | + |
| 49 | + //For first time replacement alone. |
| 50 | + if (isFirstReplace) |
| 51 | + { |
| 52 | + //Get the index of the textrange |
| 53 | + int index = ownerPara.ChildEntities.IndexOf(textRange); |
| 54 | + //Add the bookmark start before the textrange |
| 55 | + BookmarkStart bookmarkStart = new BookmarkStart(document, "Bkmk"); |
| 56 | + ownerPara.ChildEntities.Insert(index, bookmarkStart); |
| 57 | + //Increment the index |
| 58 | + index++; |
| 59 | + //Add the bookmark end after the textrange |
| 60 | + BookmarkEnd bookmarkEnd = new BookmarkEnd(document, "Bkmk"); |
| 61 | + ownerPara.ChildEntities.Insert(index + 1, bookmarkEnd); |
| 62 | + //Replace the text with HTML content |
| 63 | + document.Replace(findText, replaceDoc, true, true); |
| 64 | + //Navigate to the bookmark content |
| 65 | + BookmarksNavigator navigator = new BookmarksNavigator(document); |
| 66 | + navigator.MoveToBookmark("Bkmk"); |
| 67 | + //Get the bookmark content |
| 68 | + TextBodyPart bodyPart = navigator.GetBookmarkContent(); |
| 69 | + //Get the list of list styles |
| 70 | + GetListStyleName(bodyPart.BodyItems); |
| 71 | + //Remove the bookmark |
| 72 | + Bookmark bookmark = document.Bookmarks.FindByName("Bkmk"); |
| 73 | + document.Bookmarks.Remove(bookmark); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + //Get the next sibiling of the owner paragraph |
| 78 | + IEntity nextSibiling = ownerPara.NextSibling; |
| 79 | + //Get the owner paragraph index as start index |
| 80 | + int startIndex = ownerPara.OwnerTextBody.ChildEntities.IndexOf(ownerPara); |
| 81 | + //Replace the text with HTML content |
| 82 | + document.Replace(findText, replaceDoc, true, true); |
| 83 | + //Get the end index |
| 84 | + //If the next sibiling is present then it is the end index, else the child entities count |
| 85 | + int endIndex = nextSibiling != null ? ownerPara.OwnerTextBody.ChildEntities.IndexOf(nextSibiling) |
| 86 | + : ownerPara.OwnerTextBody.ChildEntities.Count; |
| 87 | + //Restart the numbering |
| 88 | + RestartNumbering(startIndex, endIndex, document.Sections[0].Body.ChildEntities); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + //Get the list style names from the collection |
| 93 | + private static void GetListStyleName(EntityCollection collection) |
| 94 | + { |
| 95 | + //Iterate through the collection |
| 96 | + foreach (Entity entity in collection) |
| 97 | + { |
| 98 | + switch (entity.EntityType) |
| 99 | + { |
| 100 | + //Entity is paragraph |
| 101 | + case EntityType.Paragraph: |
| 102 | + WParagraph wParagraph = (WParagraph)entity; |
| 103 | + //Check whether the paragrah has list format with numbered type which is not in the collection list. |
| 104 | + if (wParagraph.ListFormat.CurrentListLevel != null |
| 105 | + && wParagraph.ListFormat.ListType == ListType.Numbered |
| 106 | + && !listStyleNames.Contains(wParagraph.ListFormat.CurrentListStyle.Name)) |
| 107 | + //Add the list style name to the collection list |
| 108 | + listStyleNames.Add(wParagraph.ListFormat.CurrentListStyle.Name); |
| 109 | + break; |
| 110 | + //Entity is Table |
| 111 | + case EntityType.Table: |
| 112 | + WTable table = (WTable)entity; |
| 113 | + //Iterate thorugh rows |
| 114 | + foreach (WTableRow row in table.Rows) |
| 115 | + { |
| 116 | + //Iterate through cells |
| 117 | + foreach (WTableCell cell in row.Cells) |
| 118 | + { |
| 119 | + //Get the list style name |
| 120 | + GetListStyleName(cell.ChildEntities); |
| 121 | + } |
| 122 | + } |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + //Restart the numbering for replaced items |
| 128 | + private static void RestartNumbering(int startIndex, int endIndex, EntityCollection collection) |
| 129 | + { |
| 130 | + //Local value |
| 131 | + string listName = string.Empty; |
| 132 | + for (int i = startIndex; i < endIndex; i++) |
| 133 | + { |
| 134 | + Entity entity = collection[i]; |
| 135 | + switch (entity.EntityType) |
| 136 | + { |
| 137 | + //Entity is Paragraph |
| 138 | + case EntityType.Paragraph: |
| 139 | + WParagraph wParagraph = (WParagraph)entity; |
| 140 | + //Check whether the paragraph have current list level and the same list name in the collection list. |
| 141 | + if (wParagraph.ListFormat.CurrentListLevel != null |
| 142 | + && listStyleNames.Contains(wParagraph.ListFormat.CurrentListStyle.Name)) |
| 143 | + { |
| 144 | + //If the local name is not equal to current list name, then restart the numbering |
| 145 | + if (listName != wParagraph.ListFormat.CurrentListStyle.Name) |
| 146 | + { |
| 147 | + //Set the current list name as local name |
| 148 | + listName = wParagraph.ListFormat.CurrentListStyle.Name; |
| 149 | + //Enable restart numbering |
| 150 | + wParagraph.ListFormat.RestartNumbering = true; |
| 151 | + } |
| 152 | + //If the local name and current list name are equal then continue list numbering |
| 153 | + else |
| 154 | + wParagraph.ListFormat.ContinueListNumbering(); |
| 155 | + } |
| 156 | + break; |
| 157 | + //Entity is table |
| 158 | + case EntityType.Table: |
| 159 | + WTable table = (WTable)entity; |
| 160 | + //Iterate through rows |
| 161 | + foreach (WTableRow row in table.Rows) |
| 162 | + { |
| 163 | + //Iterate thorugh cells |
| 164 | + foreach (WTableCell cell in row.Cells) |
| 165 | + { |
| 166 | + //Restart numbering for child entities in the cell. |
| 167 | + RestartNumbering(0, cell.ChildEntities.Count, cell.ChildEntities); |
| 168 | + } |
| 169 | + } |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments