Skip to content

Commit ef66b4d

Browse files
ES-957458-Add-bookmark-in-HeaderFooter
1 parent 410b6a3 commit ef66b4d

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-bookmarks-in-HeaderFooter", "Add-bookmarks-in-HeaderFooter\Add-bookmarks-in-HeaderFooter.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
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+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
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>Add_bookmarks_in_HeaderFooter</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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
// Load the Word document from the specified path.
5+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
6+
{
7+
// Add bookmarks to various header and footer types in the last section of the document.
8+
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.FirstPageHeader, "FirstPageHeader");
9+
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.FirstPageFooter, "FirstPageFooter");
10+
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.OddHeader, "OddHeader");
11+
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.OddFooter, "OddFooter");
12+
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.EvenHeader, "EvenHeader");
13+
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.EvenFooter, "EvenFooter");
14+
15+
// Save the modified document to the output path in DOCX format.
16+
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
17+
}
18+
19+
/// <summary>
20+
/// Adds uniquely named bookmarks to paragraphs and table cells that contain content within the given header or footer section.
21+
/// </summary>
22+
void AddBookmarkToHeaderFooter(WordDocument document, HeaderFooter headerFooter, string bookmarkName)
23+
{
24+
int bookmarkIndex = 1; // Counter to ensure unique bookmark names
25+
26+
if (headerFooter.ChildEntities.Count > 0)
27+
{
28+
foreach (Entity childEntity in headerFooter.ChildEntities)
29+
{
30+
if (childEntity is WParagraph paragraph && paragraph.ChildEntities.Count > 0)
31+
{
32+
InsertBookmark(document, paragraph, bookmarkName + bookmarkIndex);
33+
bookmarkIndex++;
34+
}
35+
else if (childEntity is WTable table)
36+
{
37+
foreach (WTableRow row in table.Rows)
38+
{
39+
foreach (WTableCell cell in row.Cells)
40+
{
41+
foreach (Entity cellEntity in cell.ChildEntities)
42+
{
43+
if (cellEntity is WParagraph cellParagraph && cellParagraph.ChildEntities.Count > 0)
44+
{
45+
InsertBookmark(document, cellParagraph, bookmarkName + bookmarkIndex);
46+
bookmarkIndex++;
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
55+
56+
/// <summary>
57+
/// Inserts a bookmark into the given paragraph with the specified name.
58+
/// </summary>
59+
void InsertBookmark(WordDocument document, WParagraph paragraph, string name)
60+
{
61+
BookmarkStart bookmarkStart = new BookmarkStart(document, name);
62+
BookmarkEnd bookmarkEnd = new BookmarkEnd(document, name);
63+
paragraph.ChildEntities.Insert(0, bookmarkStart);
64+
paragraph.ChildEntities.Add(bookmarkEnd);
65+
}

0 commit comments

Comments
 (0)