Set the color for different levels bookmark in word documents

2015-08-31 01:15:07 Written by  support iceblue
Rate this item
(0 votes)

We have already demonstrated how to insert single bookmark to word document by using Spire.Doc. When you need to insert many bookmarks to long word document, you can also use Spire.Doc to add multiple levels bookmarks and set different colors for them. Spire.Doc Version 5.5.71 adds a new method of BookmarkLayout to enable developers to set the different color for the different levels of bookmarks. This article will show you how to set the different color for the different levels of bookmarks.

Here comes to the code snippet:

Step 1: Create a new word document and load a file with nested level bookmarks.

Document document = new Document();
document.LoadFromFile("sample.docx");

Step 2: Save the word document into PDF to view the effects clearly and add the event of BookmarkLayout before saving to PDF.

ToPdfParameterList toPdf = new ToPdfParameterList();
toPdf.CreateWordBookmarks = true;
toPdf.WordBookmarksTitle = "Changed bookmark";
toPdf.WordBookmarksColor = Color.Gray;

//the event of BookmarkLayout occurs when draw a bookmark
document.BookmarkLayout += new Spire.Doc.Documents.Rendering.BookmarkLevelHandler(document_BookmarkLayout);

document.SaveToFile("result.pdf", toPdf);

Step 3: Call the method of BookmarkLayout to set the different color for the different levels of bookmarks.

static void document_BookmarkLayout(object sender, Spire.Doc.Documents.Rendering.BookmarkLevelEventArgs args)
{
    
    //set the different color for different levels of bookmarks
    if (args.BookmarkLevel.Level == 2)
    {
        args.BookmarkLevel.Color = Color.Red;
        args.BookmarkLevel.Style = BookmarkTextStyle.Bold;
    }
    else if (args.BookmarkLevel.Level == 3)
    {
        args.BookmarkLevel.Color = Color.Gray;
        args.BookmarkLevel.Style = BookmarkTextStyle.Italic;
    }
    else
    {
        args.BookmarkLevel.Color = Color.Green;
        args.BookmarkLevel.Style = BookmarkTextStyle.Regular;
    }

Please check the effective screenshot of multiple levels bookmarks with different colors:

How to set the color for different levels bookmark in word documents

Full codes:

using Spire.Doc;
using System.Drawing;
namespace SetColor
{
    class Program
    {

        static void Main(string[] args)
        {

            Document document = new Document();
            document.LoadFromFile("sample.docx");

            ToPdfParameterList toPdf = new ToPdfParameterList();
            toPdf.CreateWordBookmarks = true;
            toPdf.WordBookmarksTitle = "Changed bookmark";
            toPdf.WordBookmarksColor = Color.Gray;

            //the event of BookmarkLayout occurs when draw a bookmark
            document.BookmarkLayout += new Spire.Doc.Documents.Rendering.BookmarkLevelHandler(document_BookmarkLayout);

            document.SaveToFile("result.pdf", toPdf);
        }
        static void document_BookmarkLayout(object sender, Spire.Doc.Documents.Rendering.BookmarkLevelEventArgs args)
        {

            if (args.BookmarkLevel.Level == 2)
            {
                args.BookmarkLevel.Color = Color.Red;
                args.BookmarkLevel.Style = BookmarkTextStyle.Bold;
            }
            else if (args.BookmarkLevel.Level == 3)
            {
                args.BookmarkLevel.Color = Color.Gray;
                args.BookmarkLevel.Style = BookmarkTextStyle.Italic;
            }
            else
            {
                args.BookmarkLevel.Color = Color.Green;
                args.BookmarkLevel.Style = BookmarkTextStyle.Regular;
            }
        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Friday, 03 September 2021 03:55