Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Bookmarks/Rename-bookmark/.NET/Rename-bookmark.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Rename-bookmark/Rename-bookmark.csproj" />
</Solution>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

93 changes: 93 additions & 0 deletions Bookmarks/Rename-bookmark/.NET/Rename-bookmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

namespace Rename_bookmark
{
class Program
{
static void Main(string[] args)
{
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Opens an existing Word document.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
{
//Replace Bookmark name
ReplaceBookmarkName(document, "Northwind", "New_Bookmark");
//Creates file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Saves the Word document to file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
}
}
#region Replace Bookmark name
Comment thread
Kathiresan4347 marked this conversation as resolved.
/// <summary>
/// Replace bookmark name
/// </summary>
/// <param name="document">Input Word document.</param>
/// <param name="existingBookmarkName">The name of the bookmark to replace.</param>
/// <param name="replaceBookmarkName">The new name for the bookmark.</param>
private static void ReplaceBookmarkName(WordDocument document, string existingBookmarkName, string replaceBookmarkName)
{
//Gets the bookmark instance by using FindByName method of BookmarkCollection with bookmark name
Bookmark bookmark = document.Bookmarks.FindByName(existingBookmarkName);
// No bookmark found, return immediately
if (bookmark == null)
return;
// Variables to store the index positions of the bookmark start and end within their respective owners
int startIndex = -1;
int endIndex = -1;
// Create new bookmark start and end markers with the replacement name
BookmarkStart newBookmarkStart = new BookmarkStart(document, replaceBookmarkName);
BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, replaceBookmarkName);

// Determine the owner and index for the bookmark start.
// The bookmark start may be inside a WParagraph (as a child entity)
// or inside an InlineContentControl (as a paragraph item).
if (bookmark.BookmarkStart != null && bookmark.BookmarkStart.Owner is WParagraph)
{
WParagraph startParagraph = bookmark.BookmarkStart.Owner as WParagraph;
// Find the index of the old bookmark start in the paragraph's child entities
startIndex = startParagraph.ChildEntities.IndexOf(bookmark.BookmarkStart);
// Insert the new bookmark end at the same index (if found)
if (startIndex >= 0)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this if needed

startParagraph.ChildEntities.Insert(startIndex, newBookmarkStart);
}
else if (bookmark.BookmarkStart != null && bookmark.BookmarkStart.Owner is InlineContentControl)
{
InlineContentControl startICC = bookmark.BookmarkStart.Owner as InlineContentControl;
// Find the index of the old bookmark end in the ICC's paragraph items
startIndex = startICC.ParagraphItems.IndexOf(bookmark.BookmarkStart);
// Insert the new bookmark end at the same index (if found)
if (startIndex >= 0)
startICC.ParagraphItems.Insert(startIndex, newBookmarkStart);
}
// Determine the owner and index for the bookmark end.
// Similar to start, the end could be in a paragraph or inline content control.
if (bookmark.BookmarkEnd != null && bookmark.BookmarkEnd.Owner is WParagraph)
{
WParagraph endParagraph = bookmark.BookmarkEnd.Owner as WParagraph;
// Find the index of the old bookmark end in the paragraph's child entities
endIndex = endParagraph.ChildEntities.IndexOf(bookmark.BookmarkEnd);
// Insert the new bookmark end at the same index (if found)
if (endIndex >= 0)
endParagraph.ChildEntities.Insert(endIndex, newBookmarkEnd);
}
else if (bookmark.BookmarkEnd != null && bookmark.BookmarkEnd.Owner is InlineContentControl)
{
InlineContentControl endICC = bookmark.BookmarkEnd.Owner as InlineContentControl;
// Find the index of the old bookmark end in the ICC's paragraph items
endIndex = endICC.ParagraphItems.IndexOf(bookmark.BookmarkEnd);
// Insert the new bookmark end at the same index (if found)
if (endIndex >= 0)
endICC.ParagraphItems.Insert(endIndex, newBookmarkEnd);
}
//Removes the bookmark from Word document.
document.Bookmarks.Remove(bookmark);
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Rename_bookmark</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

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

</Project>
Loading