How to insert bookmark for particular text in paragraph in Word document?
Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can insert bookmark for a particular text in a paragraph in the Word document in C#.
Steps to insert bookmark for a text in a paragraph in the Word document:
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
- Include the following namespace in the Program.cs file:
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
- Use the following code example to insert bookmark for a text in a paragraph in the Word document:
C#
//Open the file as a stream. using (FileStream inputStream = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //Load the file stream into a Word document. using (WordDocument document = new WordDocument(inputStream, FormatType.Docx)) { //Access paragraph in a Word document. WParagraph paragraph = document.Sections[0].Paragraphs[3] as WParagraph; //Create bookmarkstart and bookmarkend instance. BookmarkStart bookmarkStart = new BookmarkStart(document, "Northwind"); BookmarkEnd bookmarkEnd = new BookmarkEnd(document, "Northwind"); //Add bookmarkstart at index zero. paragraph.Items.Insert(0, bookmarkStart); //Add bookmarkend at index 2. paragraph.Items.Insert(2, bookmarkEnd); //Create a file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite)) { //Save the Word document to the file stream. document.Save(outputFileStream, FormatType.Docx); } } }
A complete working sample to insert bookmark for a particular text in a paragraph in the Word document in C# can be downloaded from GitHub.
By executing the program, you will get the Output document as follows.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion Word Framework features.
See Also:
How to replace the content between two different existing bookmarks in a word document
How to insert hidden bookmarks to the document
How to insert nested bookmarks to the document
How to get the bookmarks present in the document
How to add a table in a bookmark location
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.