Skip to content

Commit 4594a6e

Browse files
Get-bookmark-content-one-by-one
1 parent 983f660 commit 4594a6e

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-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}") = "Get-bookmark-content-one-by-one", "Get-bookmark-content-one-by-one\Get-bookmark-content-one-by-one.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
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>Get_bookmark_content_one_by_one</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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Get_bookmark_content_one_by_one
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+
//Loads an existing Word document into DocIO instance.
13+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
14+
{
15+
//Finds the first occurrence of a particular text in the document
16+
TextSelection textSelection = document.Find("they are considered one of the world's most loved animals.", false, true);
17+
//Gets the found text as single text range
18+
WTextRange textRange = textSelection.GetAsOneRange();
19+
//Add bookmark to the selected text range.
20+
//Create bookmarkstart and bookmarkend instance.
21+
int indexOfText = textRange.OwnerParagraph.Items.IndexOf(textRange);
22+
BookmarkStart bookmarkStart = new BookmarkStart(document, "bkmk1");
23+
BookmarkEnd bookmarkEnd = new BookmarkEnd(document, "bkmk1");
24+
//Add bookmarkstart before selected text.
25+
textRange.OwnerParagraph.Items.Insert(indexOfText, bookmarkStart);
26+
//Add bookmarkend after selected text
27+
textRange.OwnerParagraph.Items.Insert(indexOfText + 2, bookmarkEnd);
28+
textSelection = document.Find("The table below lists the main characteristics the giant panda shares with bears and red pandas.", false, true);
29+
//Gets the found text as single text range
30+
textRange = textSelection.GetAsOneRange();
31+
//Add bookmark to the selected text range.
32+
//Create bookmarkstart and bookmarkend instance.
33+
indexOfText = textRange.OwnerParagraph.Items.IndexOf(textRange);
34+
bookmarkStart = new BookmarkStart(document, "bkmk2");
35+
bookmarkEnd = new BookmarkEnd(document, "bkmk2");
36+
//Add bookmarkstart before selected text.
37+
textRange.OwnerParagraph.Items.Insert(indexOfText, bookmarkStart);
38+
//Add bookmarkend after selected text
39+
textRange.OwnerParagraph.Items.Insert(indexOfText + 2, bookmarkEnd);
40+
textSelection = document.Find("Did you know that the giant panda may actually be a raccoon", false, true);
41+
//Gets the found text as single text range
42+
textRange = textSelection.GetAsOneRange();
43+
//Add bookmark to the selected text range.
44+
//Create bookmarkstart and bookmarkend instance.
45+
indexOfText = textRange.OwnerParagraph.Items.IndexOf(textRange);
46+
bookmarkStart = new BookmarkStart(document, "bkmk3");
47+
bookmarkEnd = new BookmarkEnd(document, "bkmk3");
48+
//Add bookmarkstart before selected text.
49+
textRange.OwnerParagraph.Items.Insert(indexOfText, bookmarkStart);
50+
//Add bookmarkend after selected text
51+
textRange.OwnerParagraph.Items.Insert(indexOfText + 2, bookmarkEnd);
52+
53+
//Get all bookmarks from Word document using FindAllItemsByProperty
54+
//Find all bkmarkStart by EntityType in Word document.
55+
List<Entity> bkmarkStarts = document.FindAllItemsByProperty(EntityType.BookmarkStart, null, null);
56+
//Create an list Bookmarks of type string
57+
List<string> BookmarksContent = new List<string>();
58+
//Iterate bookmarkCollection to get the bookmark content.
59+
foreach (Entity bkmarkStart in bkmarkStarts)
60+
{
61+
BookmarkStart book = bkmarkStart as BookmarkStart;
62+
//Get the bookmark name
63+
string name = book.Name;
64+
//Creates the bookmark navigator instance to access the bookmark
65+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
66+
//Moves the virtual cursor to the location before the end of the bookmark
67+
bookmarkNavigator.MoveToBookmark(name);
68+
//Gets the bookmark content as worddocument
69+
WordDocumentPart part = bookmarkNavigator.GetContent();
70+
WordDocument tempDoc = part.GetAsWordDocument();
71+
//Get the bookmark content from the document.
72+
string text = tempDoc.GetText();
73+
//Adds the bookmark content into the list
74+
BookmarksContent.Add(text);
75+
Console.WriteLine("Bookmark content: ");
76+
Console.WriteLine(text);
77+
tempDoc.Close();
78+
tempDoc.Dispose();
79+
part.Close();
80+
81+
}
82+
Console.ReadLine();
83+
//Creates file stream.
84+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
85+
{
86+
//Saves the Word document to file stream.
87+
document.Save(outputStream, FormatType.Docx);
88+
}
89+
}
90+
}
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)