diff --git a/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter.sln b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter.sln new file mode 100644 index 000000000..a90629d2c --- /dev/null +++ b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter.sln @@ -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 diff --git a/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Add-bookmarks-in-HeaderFooter.csproj b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Add-bookmarks-in-HeaderFooter.csproj new file mode 100644 index 000000000..fca85a460 --- /dev/null +++ b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Add-bookmarks-in-HeaderFooter.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Add_bookmarks_in_HeaderFooter + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Data/Template.docx b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Data/Template.docx new file mode 100644 index 000000000..c1333065e Binary files /dev/null and b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Data/Template.docx differ diff --git a/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Output/.gitkeep b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Program.cs b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Program.cs new file mode 100644 index 000000000..241b57867 --- /dev/null +++ b/Bookmarks/Add-bookmarks-in-HeaderFooter/.NET/Add-bookmarks-in-HeaderFooter/Program.cs @@ -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); +} + +/// +/// Adds uniquely named bookmarks to paragraphs and table cells that contain content within the given header or footer section. +/// +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++; + } + } + } + } + } + } + } +} + +/// +/// Inserts a bookmark into the given paragraph with the specified name. +/// +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); +}