|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +// Define the name of the first existing bookmark from where extraction starts. |
| 7 | +string bookmark1 = "Mysteries"; |
| 8 | +// Define the name of the second existing bookmark where extraction ends. |
| 9 | +string bookmark2 = "Facts"; |
| 10 | +// Define the name for a temporary bookmark that spans content between bookmark1 and bookmark2. |
| 11 | +string tempBookmarkName = "tempBookmark"; |
| 12 | + |
| 13 | +// Load the Word document. |
| 14 | +using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"))) |
| 15 | +{ |
| 16 | + // Find the first bookmark by name. |
| 17 | + Bookmark firstBookmark = document.Bookmarks.FindByName(bookmark1); |
| 18 | + int index = 0; |
| 19 | + |
| 20 | + // Get the paragraph that contains the end of the first bookmark. |
| 21 | + WParagraph firstBookmarkOwnerPara = firstBookmark.BookmarkEnd.OwnerParagraph; |
| 22 | + // Find the position of the bookmark end in the paragraph. |
| 23 | + index = firstBookmarkOwnerPara.Items.IndexOf(firstBookmark.BookmarkEnd); |
| 24 | + |
| 25 | + // Create a temporary bookmark start and insert it right after the first bookmark end. |
| 26 | + BookmarkStart newBookmarkStart = new BookmarkStart(document, tempBookmarkName); |
| 27 | + firstBookmarkOwnerPara.ChildEntities.Insert(index + 1, newBookmarkStart); |
| 28 | + |
| 29 | + // Find the second bookmark by name. |
| 30 | + Bookmark secondBookmark = document.Bookmarks.FindByName(bookmark2); |
| 31 | + // Get the paragraph that contains the start of the second bookmark. |
| 32 | + WParagraph secondBookmarkOwnerPara = secondBookmark.BookmarkStart.OwnerParagraph; |
| 33 | + // Find the position of the bookmark start in the paragraph. |
| 34 | + index = secondBookmarkOwnerPara.Items.IndexOf(secondBookmark.BookmarkStart); |
| 35 | + |
| 36 | + // Create a temporary bookmark end and insert it just before the second bookmark start. |
| 37 | + BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, tempBookmarkName); |
| 38 | + secondBookmarkOwnerPara.ChildEntities.Insert(index, newBookmarkEnd); |
| 39 | + |
| 40 | + // Navigate to the temporary bookmark created between the two bookmarks. |
| 41 | + BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document); |
| 42 | + bookmarkNavigator.MoveToBookmark(tempBookmarkName); |
| 43 | + |
| 44 | + // Extract the content between the bookmarks as TextBodyPart (optional if needed later). |
| 45 | + TextBodyPart part = bookmarkNavigator.GetBookmarkContent(); |
| 46 | + |
| 47 | + // Get the bookmark content as a new Word document part. |
| 48 | + WordDocumentPart content = bookmarkNavigator.GetContent(); |
| 49 | + |
| 50 | + // Load the extracted content into a temporary Word document for modification or export. |
| 51 | + using (WordDocument tempDocument = content.GetAsWordDocument()) |
| 52 | + { |
| 53 | + // Remove all headers and footers from the extracted content. |
| 54 | + RemoveHeaderAndFooter(tempDocument); |
| 55 | + |
| 56 | + // Save the extracted content as an HTML file. |
| 57 | + tempDocument.Save(Path.GetFullPath(@"Output/Result.html"), FormatType.Html); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Removes all headers and footers from the given Word document. |
| 62 | +void RemoveHeaderAndFooter(WordDocument document) |
| 63 | +{ |
| 64 | + foreach (WSection section in document.Sections) |
| 65 | + { |
| 66 | + foreach (HeaderFooter entity in section.HeadersFooters) |
| 67 | + { |
| 68 | + if (entity != null) |
| 69 | + entity.ChildEntities.Clear(); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
0 commit comments