Skip to content

Commit 4d61d00

Browse files
Merge pull request #335 from SyncfusionExamples/Get-bookmark-content-one-by-one
ES-890816- Add sample Get-bookmark-content-one-by-one
2 parents 78be2af + d3072eb commit 4d61d00

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-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 17
4+
VisualStudioVersion = 17.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-text-add-bookmarks", "Find-text-add-bookmarks\Find-text-add-bookmarks.csproj", "{8B656397-21C1-40FF-87AD-988058DBC992}"
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+
{8B656397-21C1-40FF-87AD-988058DBC992}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8B656397-21C1-40FF-87AD-988058DBC992}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8B656397-21C1-40FF-87AD-988058DBC992}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8B656397-21C1-40FF-87AD-988058DBC992}.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 = {FADD9A46-011A-484B-BF35-048E0EB5CD60}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
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>Find_text_add_bookmarks</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>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace FindTextAddBookmarks
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
11+
{
12+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
13+
{
14+
// Define texts and corresponding bookmark names
15+
var textBookmarkPairs = new Dictionary<string, string>
16+
{
17+
{ "they are considered one of the world's most loved animals.", "bkmk1" },
18+
{ "The table below lists the main characteristics the giant panda shares with bears and red pandas.", "bkmk2" },
19+
{ "Did you know that the giant panda may actually be a raccoon", "bkmk3" }
20+
};
21+
22+
// Add bookmarks to specified texts
23+
foreach (var pair in textBookmarkPairs)
24+
{
25+
AddBookmarkToText(document, pair.Key, pair.Value);
26+
}
27+
28+
// Retrieve and display bookmark contents
29+
List<string> bookmarksContent = GetBookmarkContents(document);
30+
foreach (var content in bookmarksContent)
31+
{
32+
Console.WriteLine("Bookmark content: ");
33+
Console.WriteLine(content);
34+
}
35+
36+
// Save the modified document
37+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
38+
{
39+
document.Save(outputStream, FormatType.Docx);
40+
}
41+
}
42+
}
43+
}
44+
/// <summary>
45+
/// Adds a bookmark to a specific text in the document.
46+
/// </summary>
47+
private static void AddBookmarkToText(WordDocument document, string searchText, string bookmarkName)
48+
{
49+
TextSelection textSelection = document.Find(searchText, false, true);
50+
if (textSelection != null)
51+
{
52+
WTextRange textRange = textSelection.GetAsOneRange();
53+
int indexOfText = textRange.OwnerParagraph.Items.IndexOf(textRange);
54+
textRange.OwnerParagraph.Items.Insert(indexOfText, new BookmarkStart(document, bookmarkName));
55+
textRange.OwnerParagraph.Items.Insert(indexOfText + 2, new BookmarkEnd(document, bookmarkName));
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Retrieves all bookmark contents from the document.
61+
/// </summary>
62+
private static List<string> GetBookmarkContents(WordDocument document)
63+
{
64+
List<string> bookmarkContents = new List<string>();
65+
foreach (Entity entity in document.FindAllItemsByProperty(EntityType.BookmarkStart, null, null))
66+
{
67+
if (entity is BookmarkStart bookmarkStart)
68+
{
69+
var bookmarkNavigator = new BookmarksNavigator(document);
70+
bookmarkNavigator.MoveToBookmark(bookmarkStart.Name);
71+
WordDocumentPart part = bookmarkNavigator.GetContent();
72+
WordDocument tempDoc = part.GetAsWordDocument();
73+
bookmarkContents.Add(tempDoc.GetText());
74+
75+
tempDoc.Close();
76+
tempDoc.Dispose();
77+
part.Close();
78+
}
79+
}
80+
return bookmarkContents;
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)