Articles in this section
Category / Section

How to get the section number of the particular bookmark in C#?

6 mins read

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 get the section number of the particular bookmark in C#.

Steps to get the section number of the particular bookmark using C#

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org. Add DocIO.Net.Core NuGet packages of ASP.NET Core Word
  3. Include the following namespace in the Program.cs file:

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. Use the following code example to get the section number of the particular bookmark in C#.

C#

using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    //Open an existing Word document.
    using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
    {
        // Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name
        Bookmark bookmark = document.Bookmarks.FindByName("BOOKMARK2");
        if (bookmark != null)
        {
            // Get the owner section of bookmark.
            WSection section = GetOwnerEntity(bookmark.BookmarkStart) as WSection;
            if (section != null)
            {
                // Get the index value of section.
                int sectionIndex = document.ChildEntities.IndexOf(section);
                int sectionNumber = sectionIndex + 1;
                Console.WriteLine("This bookmark would be in section " + sectionNumber);
                Console.ReadKey();
            }
        }
    }                
}
  1. Helper method

C#

/// <summary>
/// Get the Entity owner.
/// </summary>
private static Entity GetOwnerEntity(BookmarkStart bookmarkStart)
{
     Entity baseEntity = bookmarkStart.Owner;
 
     while (!(baseEntity is WSection))  
     {
             if (baseEntity is null)
                    return baseEntity;
             baseEntity = baseEntity.Owner;
     }
     return baseEntity;
}                

A complete working sample to get the section number of the particular bookmark in C# can be downloaded from GitHub.

Input Word document as follows.

Input Word document in ASP.NET Core

By executing the program, you will get the output document as follows.

Output document in ASP.NET Core

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail mergemerge 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 do I insert hidden bookmarks to the document?

How to replace text within bookmark content in Word document?

Is it possible to insert table in BookMark Content?

How to replace the content between two different existing bookmarks in a word document

Conclusion

I hope you enjoyed learning about how to get the section number of the particular bookmark in C#.

You can refer to our ASP.NET Core DocIO’s feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET Core DocIO documentation to understand how to present and manipulate data.

For current customers, you can check out our ASP.NET Core 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 ASP.NET Core DocIO and other ASP.NET Core components.

 

If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied