Skip to content

Commit 68eed7d

Browse files
ES-263734-Changes added
1 parent b7da0c7 commit 68eed7d

5 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Rename-bookmark/Rename-bookmark.csproj" />
3+
</Solution>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Rename_bookmark
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
11+
{
12+
//Opens an existing Word document.
13+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
14+
{
15+
//Replace Bookmark name
16+
ReplaceBookmarkName(document, "Northwind", "New_Bookmark");
17+
//Creates file stream.
18+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite))
19+
{
20+
//Saves the Word document to file stream.
21+
document.Save(outputFileStream, FormatType.Docx);
22+
}
23+
}
24+
}
25+
}
26+
#region Replace Bookmark name
27+
/// <summary>
28+
/// Replace bookmark name
29+
/// </summary>
30+
/// <param name="document">Input Word document.</param>
31+
/// <param name="existingbookmarkName">The name of the bookmark to replace.</param>
32+
/// <param name="replaceBookmarkName">The new name for the bookmark.</param>
33+
34+
private static void ReplaceBookmarkName(WordDocument document, string existingbookmarkName, string replaceBookmarkName)
35+
{
36+
//Gets the bookmark instance by using FindByName method of BookmarkCollection with bookmark name
37+
Bookmark bookmark = document.Bookmarks.FindByName(existingbookmarkName);
38+
// No bookmark found, return immediately
39+
if (bookmark == null)
40+
return;
41+
//Gets owner paragraph of the bookmark
42+
WParagraph bookmarkStartParagraph = bookmark.BookmarkStart.Owner as WParagraph;
43+
//Gets index of the bookmark start and end
44+
int boomarkStartIndex = bookmarkStartParagraph.ChildEntities.IndexOf(bookmark.BookmarkStart);
45+
int boomarkEndIndex = 0;
46+
//Gets bookmark end paragraph
47+
WParagraph bookmarkEndParagraph = bookmark.BookmarkEnd.Owner as WParagraph;
48+
//Checks whether the bookmark start and end is in same paragraph
49+
if (bookmarkEndParagraph == bookmarkStartParagraph)
50+
boomarkEndIndex = bookmarkStartParagraph.ChildEntities.IndexOf(bookmark.BookmarkEnd);
51+
else
52+
boomarkEndIndex = bookmarkEndParagraph.ChildEntities.IndexOf(bookmark.BookmarkEnd);
53+
//Removes the bookmark from Word document.
54+
document.Bookmarks.Remove(bookmark);
55+
//Inserts new bookmark in place of deleted bookmark
56+
bookmarkStartParagraph.ChildEntities.Insert(boomarkStartIndex, bookmarkStartParagraph.AppendBookmarkStart(replaceBookmarkName));
57+
//Inserts bookmark end in corresponding paragraph.
58+
if (bookmarkEndParagraph == bookmarkStartParagraph)
59+
bookmarkStartParagraph.ChildEntities.Insert(boomarkEndIndex, bookmarkStartParagraph.AppendBookmarkEnd(replaceBookmarkName));
60+
else
61+
bookmarkEndParagraph.ChildEntities.Insert(boomarkEndIndex, bookmarkEndParagraph.AppendBookmarkEnd(replaceBookmarkName));
62+
}
63+
#endregion
64+
}
65+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Rename_bookmark</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>

0 commit comments

Comments
 (0)