Skip to content

Commit

Permalink
Added sample to clone and add a paragraph with bookmarks in the same …
Browse files Browse the repository at this point in the history
…Word document
  • Loading branch information
Suriya-Balamurugan committed Oct 28, 2024
1 parent f4b8cb0 commit 484afea
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Preserve_bookmarks_from_cloned_content</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Input.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
}
}

0 comments on commit 484afea

Please sign in to comment.