diff --git a/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content.sln b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content.sln new file mode 100644 index 00000000..a9e90f6f --- /dev/null +++ b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31911.196 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Preserve-bookmarks-from-cloned-content", "Preserve-bookmarks-from-cloned-content\Preserve-bookmarks-from-cloned-content.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4} + EndGlobalSection +EndGlobal diff --git a/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Data/Input.docx b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Data/Input.docx new file mode 100644 index 00000000..bc6f162f Binary files /dev/null and b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Data/Input.docx differ diff --git a/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Output/.gitkeep b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Output/.gitkeep new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Preserve-bookmarks-from-cloned-content.csproj b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Preserve-bookmarks-from-cloned-content.csproj new file mode 100644 index 00000000..50593da1 --- /dev/null +++ b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Preserve-bookmarks-from-cloned-content.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + Preserve_bookmarks_from_cloned_content + + + + + + + + + Always + + + Always + + + + diff --git a/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Program.cs b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Program.cs new file mode 100644 index 00000000..1259a6d9 --- /dev/null +++ b/Bookmarks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Program.cs @@ -0,0 +1,61 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using System.IO; + +namespace Preserve_bookmarks_from_cloned_content +{ + class Program + { + static void Main(string[] args) + { + using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + //Open an existing Word document. + using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic)) + { + //Create the bookmark navigator instance to access the bookmark. + BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document); + //Moves the virtual cursor to the location before the end of the bookmark "MainContent". + bookmarkNavigator.MoveToBookmark("MainContent"); + //Get the content of the "MainContent" bookmark as WordDocumentPart. + WordDocumentPart wordDocumentPart = bookmarkNavigator.GetContent(); + + //Find the "MainContent" bookmark in the document by its name. + Bookmark bookmark = document.Bookmarks.FindByName("MainContent"); + //Identify the parent text body of the bookmark. + WTextBody textbody = bookmark.BookmarkStart.OwnerParagraph.Owner as WTextBody; + //Determine the index of the paragraph containing the bookmark. + int index = textbody.ChildEntities.IndexOf(bookmark.BookmarkStart.OwnerParagraph); + + //Remove the "MainContent" bookmark from the document. + document.Bookmarks.Remove(bookmark); + //Remove inner bookmarks (SubContent1, SubContent2, SubContent3) from the document. + bookmark = document.Bookmarks.FindByName("SubContent1"); + document.Bookmarks.Remove(bookmark); + bookmark = document.Bookmarks.FindByName("SubContent2"); + document.Bookmarks.Remove(bookmark); + bookmark = document.Bookmarks.FindByName("SubContent3"); + document.Bookmarks.Remove(bookmark); + + //Insert the cloned content of the "MainContent" bookmark after the original bookmark paragraph. + if (wordDocumentPart.Sections[0].ChildEntities[0] is WTextBody) + { + WTextBody clonedTextBody = wordDocumentPart.Sections[0].ChildEntities[0] as WTextBody; + for (int i = 0, j = index + 1; i < clonedTextBody.ChildEntities.Count; i++, j++) + { + textbody.ChildEntities.Insert(j, clonedTextBody.ChildEntities[i]); + } + } + //Close the WordDocumentPart instance. + wordDocumentPart.Close(); + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the Word document to file stream. + document.Save(outputFileStream, FormatType.Docx); + } + } + } + } + } +}