Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jul 23, 2015 9:42 pm

The top level bookmark says "Word Bookmarks". I want it to say the title of my report.

How do I change the text?

Also is there a way to change the color of the bookmarks? Right now they are dark orange or brown.

Thank you.

[email protected]
 
Posts: 110
Joined: Tue May 19, 2015 8:09 pm

Fri Jul 24, 2015 3:26 am

Hello,

Thanks for your inquiry.
Sorry that the Spire.Doc doesn’t support to change the bookmark title ("Word Bookmarks") and change bookmark color. You can use Spire.PDF to achieve these requirements. And there is code to change the bookmark title and color:
Code: Select all
PdfDocument doc = new PdfDocument(@"F:\testing\form\bin\Debug\sample.pdf");
            PdfBookmarkCollection bookmarks = doc.Bookmarks;
           
            if (bookmarks.Count > 0)
            {             
                foreach (PdfBookmark bookmark in bookmarks)
                {
                   
                    if (bookmark.Title == "Word Bookmarks")
                    {
                    // set the color of the bookmark
                    bookmark.Color = Color.Blue;
                    bookmark.DisplayStyle = PdfTextStyle.Bold;

                    // change "Word Bookmarks" to "Changed bookmark"
                    bookmark.Title = "Changed bookmark";
                   
                    // change children of Parent Bookmark
                    EditChildren1Bookmark(bookmark);
                    }
                    else
                    {
                    bookmark.Color = Color.Blue;
                    bookmark.DisplayStyle = PdfTextStyle.Bold;
                    EditChildren1Bookmark(bookmark);
                    }                 
                }
            }           
            doc.SaveToFile("editbookmark.pdf");
            System.Diagnostics.Process.Start("editbookmark.pdf");
        }
        static void EditChildren1Bookmark(PdfBookmark parentBookmark)
        {
            if (parentBookmark.Count > 0)
            {
                foreach (PdfBookmark childBookmark in parentBookmark)
                {
                    childBookmark.Color = Color.Red;
                    childBookmark.DisplayStyle = PdfTextStyle.Regular;
                    EditChildren2Bookmark(childBookmark);
                }
            }
        }
        static void EditChildren2Bookmark(PdfBookmark child2Bookmark)
        {
            if (child2Bookmark.Count > 0)
            {
                foreach (PdfBookmark childBookmark in child2Bookmark)
                {
                    childBookmark.Color = Color.Yellow;
                    childBookmark.DisplayStyle = PdfTextStyle.Regular;                   
                }
            }
        }

If I miss something, please let me know.

Best Regards,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Fri Jul 24, 2015 5:58 am

Hello,

We have added the new features you mentioned into our schedule. We will let you know as soon as the new features are done.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Fri Jul 24, 2015 7:02 pm

Thank you. That code worked fine for me, but I really need it in Spire.DOC.dll

Let me know when the hot fix is available.

[email protected]
 
Posts: 110
Joined: Tue May 19, 2015 8:09 pm

Thu Jul 30, 2015 4:42 pm

Betsy, any idea how long it will be until you get this feature added to Spire.Doc? I really need to put this into production.

Thank you.

[email protected]
 
Posts: 110
Joined: Tue May 19, 2015 8:09 pm

Fri Jul 31, 2015 6:57 am

Hello,

Thanks for your waiting. Please use this version and try. Here is code for your reference:
Code: Select all
ToPdfParameterList list = new ToPdfParameterList();
list.CreateWordBookmarks = true;
list.WordBookmarksTitle = "Changed bookmark";
list.WordBookmarksColor = Color.Blue;
list.WordBookmarksTextStyle = BookmarkTextStyle.Bold;         

BookmarkStart bs =   paragraph.AppendBookmarkStart("SimpleBookMark");
bs.Color = Color.Red;
bs.Style = BookmarkTextStyle.Bold;


If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Fri Jul 31, 2015 2:00 pm

Betsy, I need to loop through the bookmarks and color them depending on the bookmark name like I am doing with my trial version of spire.pdf. Can I do this in the new version of spire.doc that you posted for me?

Thank you.

Code: Select all
private static void RenameAndColorBookmarks(string pdfFileName)
{
   PdfDocument doc = new PdfDocument(pdfFileName);

   PdfBookmark topLevelBookmark = doc.Bookmarks[0]; //usually there is only one top level bookmark

   if (topLevelBookmark != null)
   {
      if (topLevelBookmark.Title == "Word Bookmarks")
      {
         topLevelBookmark.Title = TmrData.Report.ReportName;
         topLevelBookmark.Color = Color.Red;
         topLevelBookmark.DisplayStyle = PdfTextStyle.Bold;

         foreach (PdfBookmark bookmark in topLevelBookmark)
         {
            bookmark.Color = Color.Blue;
            bookmark.DisplayStyle = PdfTextStyle.Bold;

            if (bookmark.Title.ToUpper() == "DETAILS OF OUTSTANDING DEBT")
            {
               if (bookmark.Count > 0)
               {
                  foreach (PdfBookmark bondBookmark in bookmark)
                  {
                     bondBookmark.Color = Color.Green;
                     bondBookmark.DisplayStyle = PdfTextStyle.Bold;
                  }
               }
            }
         }
      }
   }

   doc.SaveToFile(pdfFileName);
}


[email protected]
 
Posts: 110
Joined: Tue May 19, 2015 8:09 pm

Mon Aug 03, 2015 2:06 am

Hello,

Sorry for late reply as weekends.
You can color bookmarks depending on the bookmark name by using new version of spire.doc. There are codes for your reference:
Code: Select all
Document doc = new Document("sample.docx");
            BookmarkCollection bookmarks = doc.Bookmarks;
            foreach (Bookmark bookmark in bookmarks)
            {
                if (bookmark.Name == "DETAILS OF OUTSTANDING DEBT")
                {
                    bookmark.BookmarkStart.Color = Color.Blue;
                    bookmark.BookmarkStart.Style = BookmarkTextStyle.Bold;
                }

                else
                {
                    bookmark.BookmarkStart.Color = Color.Green;
                    bookmark.BookmarkStart.Style = BookmarkTextStyle.Regular;
                }       
            }

            ToPdfParameterList list = new ToPdfParameterList();
            list.CreateWordBookmarks = true;
            list.WordBookmarksTitle = "Changed bookmark";

            doc.SaveToFile("sample.pdf", list);


If there is any question, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Mon Aug 03, 2015 8:23 pm

Betsy, the bookmark.BookmarkStart.ChildObjects.Count always return 0, even though there are children.

The code that I posted ( Fri Jul 31, 2015 9:00 am ) works with spire.pdf, but when I convert the code to work with Spire.Doc, the childObjects are not getting populated.

This is the code I am running and I have uploaded 2 pictures. The first picture show it NOT working with spire.doc, the second is spire.pdf that does work.

Code: Select all
public static void TMRtoPDF(TMRData tmrData, string pdfFileName)
{
   TmrData = tmrData;
   Document document = BuildDoc();
   ColorBookmarks(document);

   ToPdfParameterList toPdf = new ToPdfParameterList();
   toPdf.CreateWordBookmarks = true;
   toPdf.WordBookmarksTitle = TmrData.Report.ReportName;
   toPdf.WordBookmarksColor = Color.Red;
   toPdf.WordBookmarksTextStyle = BookmarkTextStyle.Bold;
   document.SaveToFile(pdfFileName, toPdf);
}

private static void ColorBookmarks(Document document)
{
   BookmarkCollection bookmarks = document.Bookmarks;

   foreach (Bookmark bookmark in bookmarks)
   {
      bookmark.BookmarkStart.Color = Color.Blue;
      bookmark.BookmarkStart.Style = BookmarkTextStyle.Bold;

      if (bookmark.BookmarkStart.Name.ToUpper() == "DETAILS OF OUTSTANDING DEBT")
      {
         if (bookmark.BookmarkStart.ChildObjects.Count > 0)
         {
            foreach (Bookmark debtBookmark in bookmark.BookmarkStart.ChildObjects)
            {
               debtBookmark.BookmarkStart.Color = Color.Green;
               debtBookmark.BookmarkStart.Style = BookmarkTextStyle.Bold;
            }
         }
      }
   }
}



[email protected]
 
Posts: 110
Joined: Tue May 19, 2015 8:09 pm

Tue Aug 04, 2015 2:37 am

Hello,

Thanks for your reply. Please note that there is difference between Spire.PDF and Spire.Doc.
In Spire.Doc the BookmarkCollection collects all bookmarks, not just parent bookmarks, so there is no way to judge if the bookmark has children with Spire.Doc. The bookmark.BookmarkStart.ChildObjects.Count always returns 0. Sorry for inconvenience.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Tue Aug 04, 2015 4:26 pm

>> The bookmark.BookmarkStart.ChildObjects.Count always returns 0. Sorry for inconvenience.

Well are you going to suggest to the development team that they should respect all bookmarks and note that some bookmarks have children? It seems like a bug.

[email protected]
 
Posts: 110
Joined: Tue May 19, 2015 8:09 pm

Wed Aug 05, 2015 2:44 am

Hello,

Thanks for your replay. I had got information from our Dev team that there is no way to judge if a bookmark has a child, the reason is that in MS Word all bookmarks are same level.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Wed Aug 05, 2015 2:31 pm

Betsy,

>> if a bookmark has a child, the reason is that in MS Word all bookmarks are same level.

1. Then how can they be displayed on different levels (child bookmarks)?
2. Why is there even a ChildObjects on the bookmark? --bookmark.BookmarkStart.ChildObjects

There must be a communication breakdown, can you talk with the Dev team supervisor?

Thank you for your help.

[email protected]
 
Posts: 110
Joined: Tue May 19, 2015 8:09 pm

Thu Aug 06, 2015 8:58 am

Hello,

Thanks for your feedback. Today I have discussed with our Dev team supervisor again, it is possible to add new method to set the color according to the bookmark level. Now it is in processing, we will let you know, once there is new progress.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Thu Aug 06, 2015 2:22 pm

Thank you Betsy.

[email protected]
 
Posts: 110
Joined: Tue May 19, 2015 8:09 pm

Return to Spire.Doc