Skip to content

Commit 725db94

Browse files
committed
Added sample
1 parent 4d6f15f commit 725db94

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-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.14.36518.9 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-bookmarks-to-all-paragraphs-and-retrieve-contents", "Add-bookmarks-to-all-paragraphs-and-retrieve-contents\Add-bookmarks-to-all-paragraphs-and-retrieve-contents.csproj", "{B2C967AD-01D4-483D-BF02-408BF34FB556}"
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+
{B2C967AD-01D4-483D-BF02-408BF34FB556}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B2C967AD-01D4-483D-BF02-408BF34FB556}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B2C967AD-01D4-483D-BF02-408BF34FB556}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B2C967AD-01D4-483D-BF02-408BF34FB556}.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 = {05F71308-F810-4B65-AA90-DE1D11EEF13C}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Add_bookmarks_to_all_paragraphs_and_retrieve_contents</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\Input.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Syncfusion.DocIO.DLS;
2+
3+
namespace Add_bookmarks_to_all_paragraphs_and_retrieve_contents
4+
{
5+
class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
// Load the existing Word document
10+
WordDocument document = new WordDocument(Path.GetFullPath("Data/Input.docx"));
11+
// Retrieve all paragraph entities in the document
12+
List<Entity> paragraphsToInsertBookmarks = document.FindAllItemsByProperty(EntityType.Paragraph, null, null);
13+
foreach (Entity entity in paragraphsToInsertBookmarks)
14+
{
15+
// Cast the entity to a paragraph
16+
WParagraph currentPara = entity as WParagraph;
17+
// Skip the paragraph if it is empty
18+
if (currentPara.Text != string.Empty)
19+
{
20+
// Create a unique bookmark name using a GUID
21+
string bookmarkName = "Bookmark" + Guid.NewGuid();
22+
// Insert a bookmark start at the beginning of the paragraph
23+
currentPara.ChildEntities.Insert(0, new BookmarkStart(document, bookmarkName));
24+
// Insert a bookmark end at the end of the paragraph
25+
currentPara.AppendBookmarkEnd(bookmarkName);
26+
}
27+
}
28+
// Retrieve the contents of all bookmarks in the document
29+
Dictionary<string, string> bookmarkContents = GetBookmarkContents(document);
30+
// Print each bookmark name and its corresponding content
31+
foreach (string bkmkName in bookmarkContents.Keys)
32+
{
33+
Console.WriteLine("Corresponding Bookmark : " + bkmkName);
34+
Console.WriteLine("Content : " + bookmarkContents[bkmkName]);
35+
}
36+
Console.ReadLine();
37+
}
38+
/// <summary>
39+
/// Retrieves all bookmark contents from the document.
40+
/// </summary>
41+
/// <param name="document">The Word document containing bookmarks.</param>
42+
/// <returns>A dictionary with bookmark names as keys and their text content as values.</returns>
43+
private static Dictionary<string, string> GetBookmarkContents(WordDocument document)
44+
{
45+
// Create a dictionary to store bookmark names and their contents
46+
Dictionary<string, string> bookmarkContents = new Dictionary<string, string>();
47+
// Iterate through each bookmark
48+
foreach (Bookmark currentBookmark in document.Bookmarks)
49+
{
50+
// Create navigator to move to the current bookmark
51+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
52+
bookmarkNavigator.MoveToBookmark(currentBookmark.Name);
53+
// Extract the content inside the bookmark as a temporary Word document
54+
WordDocument tempDoc = bookmarkNavigator.GetContent().GetAsWordDocument();
55+
// Get the text content and add it to the dictionary
56+
bookmarkContents.Add(currentBookmark.Name, tempDoc.GetText());
57+
// Close the temporary document.
58+
tempDoc.Close();
59+
}
60+
// Return the dictionary containing all bookmark contents
61+
return bookmarkContents;
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)