diff --git a/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list.sln b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list.sln new file mode 100644 index 000000000..f01040a26 --- /dev/null +++ b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-with-HTML-list", "Replace-text-with-HTML-list\Replace-text-with-HTML-list.csproj", "{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {ADDC6FEE-8718-4B00-B091-6C4DC0CBF62A} + EndGlobalSection +EndGlobal diff --git a/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Data/Template.docx b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Data/Template.docx new file mode 100644 index 000000000..af20da1a9 Binary files /dev/null and b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Data/Template.docx differ diff --git a/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Data/sample.html b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Data/sample.html new file mode 100644 index 000000000..30766c18b --- /dev/null +++ b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Data/sample.html @@ -0,0 +1,21 @@ +

+
+

+
    +
  1. +

    +Mountain 200 +

    +
  2. +
  3. +

    + +Mountain 300 +
    +
    +

    +
  4. +
+

+
+

diff --git a/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Output/.gitkeep b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Program.cs b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Program.cs new file mode 100644 index 000000000..c6e610479 --- /dev/null +++ b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Program.cs @@ -0,0 +1,175 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; + +namespace Replace_text_with_HTML_list +{ + internal class Program + { + // List to store the names of different list styles used in the document. + static List listStyleNames = new List(); + static void Main(string[] args) + { + // Load the input Word document from file stream + using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read)) + { + // Open the Word document + using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) + { + // Load the input Word document from file stream + using (FileStream htmlStream = new FileStream(Path.GetFullPath(@"Data/sample.html"), FileMode.Open, FileAccess.Read)) + { + // Open the Word document + using (WordDocument replaceDoc = new WordDocument(htmlStream, FormatType.Html)) + { + //Replace the first word with HTML file content + ReplaceText(document, "Tag1", replaceDoc, true); + //Replace the second word with HTML file content + ReplaceText(document, "Tag2", replaceDoc, false); + + // Save the modified document to a new file + using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write)) + { + document.Save(docStream1, FormatType.Docx); + } + } + } + } + } + } + private static void ReplaceText(WordDocument document, string findText, WordDocument replaceDoc, bool isFirstReplace) + { + TextSelection selection = document.Find(findText, true, true); + if (selection != null) + { + //Get the textrange + WTextRange textRange = selection.GetAsOneRange(); + //Get the owner paragraph + WParagraph ownerPara = textRange.OwnerParagraph; + + //For first time replacement alone. + if (isFirstReplace) + { + //Get the index of the textrange + int index = ownerPara.ChildEntities.IndexOf(textRange); + //Add the bookmark start before the textrange + BookmarkStart bookmarkStart = new BookmarkStart(document, "Bkmk"); + ownerPara.ChildEntities.Insert(index, bookmarkStart); + //Increment the index + index++; + //Add the bookmark end after the textrange + BookmarkEnd bookmarkEnd = new BookmarkEnd(document, "Bkmk"); + ownerPara.ChildEntities.Insert(index + 1, bookmarkEnd); + //Replace the text with HTML content + document.Replace(findText, replaceDoc, true, true); + //Navigate to the bookmark content + BookmarksNavigator navigator = new BookmarksNavigator(document); + navigator.MoveToBookmark("Bkmk"); + //Get the bookmark content + TextBodyPart bodyPart = navigator.GetBookmarkContent(); + //Get the list of list styles + GetListStyleName(bodyPart.BodyItems); + //Remove the bookmark + Bookmark bookmark = document.Bookmarks.FindByName("Bkmk"); + document.Bookmarks.Remove(bookmark); + } + else + { + //Get the next sibiling of the owner paragraph + IEntity nextSibiling = ownerPara.NextSibling; + //Get the owner paragraph index as start index + int startIndex = ownerPara.OwnerTextBody.ChildEntities.IndexOf(ownerPara); + //Replace the text with HTML content + document.Replace(findText, replaceDoc, true, true); + //Get the end index + //If the next sibiling is present then it is the end index, else the child entities count + int endIndex = nextSibiling != null ? ownerPara.OwnerTextBody.ChildEntities.IndexOf(nextSibiling) + : ownerPara.OwnerTextBody.ChildEntities.Count; + //Restart the numbering + RestartNumbering(startIndex, endIndex, document.Sections[0].Body.ChildEntities); + } + } + } + //Get the list style names from the collection + private static void GetListStyleName(EntityCollection collection) + { + //Iterate through the collection + foreach (Entity entity in collection) + { + switch (entity.EntityType) + { + //Entity is paragraph + case EntityType.Paragraph: + WParagraph wParagraph = (WParagraph)entity; + //Check whether the paragrah has list format with numbered type which is not in the collection list. + if (wParagraph.ListFormat.CurrentListLevel != null + && wParagraph.ListFormat.ListType == ListType.Numbered + && !listStyleNames.Contains(wParagraph.ListFormat.CurrentListStyle.Name)) + //Add the list style name to the collection list + listStyleNames.Add(wParagraph.ListFormat.CurrentListStyle.Name); + break; + //Entity is Table + case EntityType.Table: + WTable table = (WTable)entity; + //Iterate thorugh rows + foreach (WTableRow row in table.Rows) + { + //Iterate through cells + foreach (WTableCell cell in row.Cells) + { + //Get the list style name + GetListStyleName(cell.ChildEntities); + } + } + break; + } + } + } + //Restart the numbering for replaced items + private static void RestartNumbering(int startIndex, int endIndex, EntityCollection collection) + { + //Local value + string listName = string.Empty; + for (int i = startIndex; i < endIndex; i++) + { + Entity entity = collection[i]; + switch (entity.EntityType) + { + //Entity is Paragraph + case EntityType.Paragraph: + WParagraph wParagraph = (WParagraph)entity; + //Check whether the paragraph have current list level and the same list name in the collection list. + if (wParagraph.ListFormat.CurrentListLevel != null + && listStyleNames.Contains(wParagraph.ListFormat.CurrentListStyle.Name)) + { + //If the local name is not equal to current list name, then restart the numbering + if (listName != wParagraph.ListFormat.CurrentListStyle.Name) + { + //Set the current list name as local name + listName = wParagraph.ListFormat.CurrentListStyle.Name; + //Enable restart numbering + wParagraph.ListFormat.RestartNumbering = true; + } + //If the local name and current list name are equal then continue list numbering + else + wParagraph.ListFormat.ContinueListNumbering(); + } + break; + //Entity is table + case EntityType.Table: + WTable table = (WTable)entity; + //Iterate through rows + foreach (WTableRow row in table.Rows) + { + //Iterate thorugh cells + foreach (WTableCell cell in row.Cells) + { + //Restart numbering for child entities in the cell. + RestartNumbering(0, cell.ChildEntities.Count, cell.ChildEntities); + } + } + break; + } + } + } + } +} diff --git a/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Replace-text-with-HTML-list.csproj b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Replace-text-with-HTML-list.csproj new file mode 100644 index 000000000..433ce2860 --- /dev/null +++ b/Find-and-Replace/Replace-text-with-HTML-list/.NET/Replace-text-with-HTML-list/Replace-text-with-HTML-list.csproj @@ -0,0 +1,27 @@ + + + + Exe + net8.0 + Replace_text_with_HTML_list + enable + enable + + + + + + + + + Always + + + Always + + + Always + + + +