|
| 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