How to export content between two bookmarks as HTML in a Word document?
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can export content between two bookmarks as HTML in a Word document using C#.
To achieve this, insert a temporary bookmark between two existing bookmarks, extract the content using BookmarksNavigator, and save it as an HTML file.
Steps to export content between two bookmarks as HTML in a Word document:
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
- Use the following code example to export content between two bookmarks as HTML in a Word document.
C#
// Define the name of the first existing bookmark from where extraction starts.
string bookmark1 = "Mysteries";
// Define the name of the second existing bookmark where extraction ends.
string bookmark2 = "Facts";
// Define the name for a temporary bookmark that spans content between bookmark1 and bookmark2.
string tempBookmarkName = "tempBookmark";
// Load the Word document.
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{
// Find the first bookmark by name.
Bookmark firstBookmark = document.Bookmarks.FindByName(bookmark1);
int index = 0;
// Get the paragraph that contains the end of the first bookmark.
WParagraph firstBookmarkOwnerPara = firstBookmark.BookmarkEnd.OwnerParagraph;
// Find the position of the bookmark end in the paragraph.
index = firstBookmarkOwnerPara.Items.IndexOf(firstBookmark.BookmarkEnd);
// Create a temporary bookmark start and insert it right after the first bookmark end.
BookmarkStart newBookmarkStart = new BookmarkStart(document, tempBookmarkName);
firstBookmarkOwnerPara.ChildEntities.Insert(index + 1, newBookmarkStart);
// Find the second bookmark by name.
Bookmark secondBookmark = document.Bookmarks.FindByName(bookmark2);
// Get the paragraph that contains the start of the second bookmark.
WParagraph secondBookmarkOwnerPara = secondBookmark.BookmarkStart.OwnerParagraph;
// Find the position of the bookmark start in the paragraph.
index = secondBookmarkOwnerPara.Items.IndexOf(secondBookmark.BookmarkStart);
// Create a temporary bookmark end and insert it just before the second bookmark start.
BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, tempBookmarkName);
secondBookmarkOwnerPara.ChildEntities.Insert(index, newBookmarkEnd);
// Navigate to the temporary bookmark created between the two bookmarks.
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark(tempBookmarkName);
// Extract the content between the bookmarks as TextBodyPart (optional if needed later).
TextBodyPart part = bookmarkNavigator.GetBookmarkContent();
// Get the bookmark content as a new Word document part.
WordDocumentPart content = bookmarkNavigator.GetContent();
// Load the extracted content into a temporary Word document for modification or export.
using (WordDocument tempDocument = content.GetAsWordDocument())
{
// Remove all headers and footers from the extracted content.
RemoveHeaderAndFooter(tempDocument);
// Save the extracted content as an HTML file.
tempDocument.Save(Path.GetFullPath(@"Output/Result.html"), FormatType.Html);
}
}
- Use the following code example to remove all headers and footers from the given Word document.
C#
void RemoveHeaderAndFooter(WordDocument document)
{
foreach (WSection section in document.Sections)
{
foreach (HeaderFooter entity in section.HeadersFooters)
{
if (entity != null)
entity.ChildEntities.Clear();
}
}
}
You can download a complete working sample to export content between two bookmarks as HTML in a Word document from GitHub.
Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to export content between two bookmarks as HTML in a Word document in a .NET Core Word document.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!