Skip to content

Commit 484afea

Browse files
Added sample to clone and add a paragraph with bookmarks in the same Word document
1 parent f4b8cb0 commit 484afea

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Preserve_bookmarks_from_cloned_content</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Input.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.IO;
4+
5+
namespace Preserve_bookmarks_from_cloned_content
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
12+
{
13+
//Open an existing Word document.
14+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
15+
{
16+
//Create the bookmark navigator instance to access the bookmark.
17+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
18+
//Moves the virtual cursor to the location before the end of the bookmark "MainContent".
19+
bookmarkNavigator.MoveToBookmark("MainContent");
20+
//Get the content of the "MainContent" bookmark as WordDocumentPart.
21+
WordDocumentPart wordDocumentPart = bookmarkNavigator.GetContent();
22+
23+
//Find the "MainContent" bookmark in the document by its name.
24+
Bookmark bookmark = document.Bookmarks.FindByName("MainContent");
25+
//Identify the parent text body of the bookmark.
26+
WTextBody textbody = bookmark.BookmarkStart.OwnerParagraph.Owner as WTextBody;
27+
//Determine the index of the paragraph containing the bookmark.
28+
int index = textbody.ChildEntities.IndexOf(bookmark.BookmarkStart.OwnerParagraph);
29+
30+
//Remove the "MainContent" bookmark from the document.
31+
document.Bookmarks.Remove(bookmark);
32+
//Remove inner bookmarks (SubContent1, SubContent2, SubContent3) from the document.
33+
bookmark = document.Bookmarks.FindByName("SubContent1");
34+
document.Bookmarks.Remove(bookmark);
35+
bookmark = document.Bookmarks.FindByName("SubContent2");
36+
document.Bookmarks.Remove(bookmark);
37+
bookmark = document.Bookmarks.FindByName("SubContent3");
38+
document.Bookmarks.Remove(bookmark);
39+
40+
//Insert the cloned content of the "MainContent" bookmark after the original bookmark paragraph.
41+
if (wordDocumentPart.Sections[0].ChildEntities[0] is WTextBody)
42+
{
43+
WTextBody clonedTextBody = wordDocumentPart.Sections[0].ChildEntities[0] as WTextBody;
44+
for (int i = 0, j = index + 1; i < clonedTextBody.ChildEntities.Count; i++, j++)
45+
{
46+
textbody.ChildEntities.Insert(j, clonedTextBody.ChildEntities[i]);
47+
}
48+
}
49+
//Close the WordDocumentPart instance.
50+
wordDocumentPart.Close();
51+
//Create file stream.
52+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite))
53+
{
54+
//Save the Word document to file stream.
55+
document.Save(outputFileStream, FormatType.Docx);
56+
}
57+
}
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)