Skip to content
Merged
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,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-bookmarks-in-HeaderFooter", "Add-bookmarks-in-HeaderFooter\Add-bookmarks-in-HeaderFooter.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

// Load the Word document from the specified path.
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{
// Add bookmarks to various header and footer types in the last section of the document.
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.FirstPageHeader, "FirstPageHeader");
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.FirstPageFooter, "FirstPageFooter");
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.OddHeader, "OddHeader");
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.OddFooter, "OddFooter");
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.EvenHeader, "EvenHeader");
AddBookmarkToHeaderFooter(document, document.LastSection.HeadersFooters.EvenFooter, "EvenFooter");

// Save the modified document to the output path in DOCX format.
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
}

/// <summary>
/// Adds uniquely named bookmarks to paragraphs and table cells that contain content within the given header or footer section.
/// </summary>
void AddBookmarkToHeaderFooter(WordDocument document, HeaderFooter headerFooter, string bookmarkName)
{
int bookmarkIndex = 1; // Counter to ensure unique bookmark names

if (headerFooter.ChildEntities.Count > 0)
{
foreach (Entity childEntity in headerFooter.ChildEntities)
{
if (childEntity is WParagraph paragraph && paragraph.ChildEntities.Count > 0)
{
InsertBookmark(document, paragraph, bookmarkName + bookmarkIndex);
bookmarkIndex++;
}
else if (childEntity is WTable table)
{
foreach (WTableRow row in table.Rows)
{
foreach (WTableCell cell in row.Cells)
{
foreach (Entity cellEntity in cell.ChildEntities)
{
if (cellEntity is WParagraph cellParagraph && cellParagraph.ChildEntities.Count > 0)
{
InsertBookmark(document, cellParagraph, bookmarkName + bookmarkIndex);
bookmarkIndex++;
}
}
}
}
}
}
}
}

/// <summary>
/// Inserts a bookmark into the given paragraph with the specified name.
/// </summary>
void InsertBookmark(WordDocument document, WParagraph paragraph, string name)
{
BookmarkStart bookmarkStart = new BookmarkStart(document, name);
BookmarkEnd bookmarkEnd = new BookmarkEnd(document, name);
paragraph.ChildEntities.Insert(0, bookmarkStart);
paragraph.ChildEntities.Add(bookmarkEnd);
}
Loading