-
Notifications
You must be signed in to change notification settings - Fork 56
ES-263734-Added DocIO Sample for rename bookmark in a word document #506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68eed7d
ES-263734-Changes added
Kathiresan4347 dcbc771
ES-263734-Changes added
Kathiresan4347 9652fcd
ES-263734-Changes added
Kathiresan4347 09bbcdc
ES-263734-Feedback addressed
Kathiresan4347 ac7c2ac
ES-263734-Comments modified
Kathiresan4347 2f74cb7
ES-263734-Feedback addressed
Kathiresan4347 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| 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 | ||
| /// <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. | ||
| 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. | ||
| 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. | ||
| 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. | ||
| endICC.ParagraphItems.Insert(endIndex, newBookmarkEnd); | ||
| } | ||
| //Removes the bookmark from Word document. | ||
| document.Bookmarks.Remove(bookmark); | ||
| } | ||
| #endregion | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
Bookmarks/Rename-bookmark/.NET/Rename-bookmark/Rename-bookmark.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.