Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36518.9 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B2C967AD-01D4-483D-BF02-408BF34FB556}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B2C967AD-01D4-483D-BF02-408BF34FB556}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2C967AD-01D4-483D-BF02-408BF34FB556}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2C967AD-01D4-483D-BF02-408BF34FB556}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {05F71308-F810-4B65-AA90-DE1D11EEF13C}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<None Update="Data\Input.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Syncfusion.DocIO.DLS;

namespace Add_bookmarks_to_all_paragraphs_and_retrieve_contents
{
class Program
{
public static void Main(string[] args)
{
// Load the existing Word document
WordDocument document = new WordDocument(Path.GetFullPath("Data/Input.docx"));
// Retrieve all paragraph entities in the document
List<Entity> paragraphsToInsertBookmarks = document.FindAllItemsByProperty(EntityType.Paragraph, null, null);
foreach (Entity entity in paragraphsToInsertBookmarks)
{
// Cast the entity to a paragraph
WParagraph currentPara = entity as WParagraph;
// Skip the paragraph if it is empty
if (currentPara.Text != string.Empty)
{
// Create a unique bookmark name using a GUID
string bookmarkName = "Bookmark" + Guid.NewGuid();
// Insert a bookmark start at the beginning of the paragraph
currentPara.ChildEntities.Insert(0, new BookmarkStart(document, bookmarkName));
// Insert a bookmark end at the end of the paragraph
currentPara.AppendBookmarkEnd(bookmarkName);
}
}
// Retrieve the contents of all bookmarks in the document
Dictionary<string, string> bookmarkContents = GetBookmarkContents(document);
// Print each bookmark name and its corresponding content
foreach (string bkmkName in bookmarkContents.Keys)
{
Console.WriteLine("Corresponding Bookmark : " + bkmkName);
Console.WriteLine("Content : " + bookmarkContents[bkmkName]);
}
Console.ReadLine();
}
/// <summary>
/// Retrieves all bookmark contents from the document.
/// </summary>
/// <param name="document">The Word document containing bookmarks.</param>
/// <returns>A dictionary with bookmark names as keys and their text content as values.</returns>
private static Dictionary<string, string> GetBookmarkContents(WordDocument document)
{
// Create a dictionary to store bookmark names and their contents
Dictionary<string, string> bookmarkContents = new Dictionary<string, string>();
// Iterate through each bookmark
foreach (Bookmark currentBookmark in document.Bookmarks)
{
// Create navigator to move to the current bookmark
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark(currentBookmark.Name);
// Extract the content inside the bookmark as a temporary Word document
WordDocument tempDoc = bookmarkNavigator.GetContent().GetAsWordDocument();
// Get the text content and add it to the dictionary
bookmarkContents.Add(currentBookmark.Name, tempDoc.GetText());
// Close the temporary document.
tempDoc.Close();
}
// Return the dictionary containing all bookmark contents
return bookmarkContents;
}
}
}
Loading