Spire.Doc 教程:如何在單詞文檔中設置不一樣級別書籤的顏色

當須要將許多書籤插入長字文檔時,變可使用Spire.Doc添加多個級別的書籤併爲其設置不一樣的顏色。Spire.Doc版本5.5.71添加了一種新的BookmarkLayout方法,使開發人員可以爲不一樣級別的書籤設置不一樣的顏色。html

圖片1

代碼段:htm

Step 1: 建立一個新的單詞文檔並加載具備嵌套級別書籤的文件。blog

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

Step 2:將單詞文檔保存爲PDF,以清晰地查看效果,並在保存爲PDF以前添加BookmarkLayout的事件。事件

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:調用BookmarkLayout的方法爲不一樣級別的書籤設置不一樣的顏色。圖片

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;

檢查不一樣顏色多級書籤的效果截圖:開發

圖片2

完整代碼:文檔

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;
        }
      }
  }

慧都控件網get

相關文章
相關標籤/搜索