C++: Add or Remove Bookmarks in Word Documents

A bookmark in a Word document enables you to navigate to any specific place within the document without having to scroll through large blocks of text. It functions just like an internal link between sections of your document. Bookmarks are particularly useful for navigating through lengthy documents. In this article, you will learn how to add or remove bookmarks in Word documents in C++ using Spire.Doc for C++.

Install Spire.Doc for C++

There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.Doc for C++ in a C++ Application

Add a Bookmark to a Paragraph in C++

Bookmarks are usually created based on whole paragraphs, especially when the paragraphs themselves are headings. The following are the steps to add a bookmark to a paragraph using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Get a specific section using Document->GetSections()->GetItem() method.
  • Get a specific paragraph from the section using Section->GetParagraphs()->GetItem() method.
  • Create a BookmarkStart object, and insert it at the beginning of the paragraph using Paragraph->GetChildObjects()->Insert(int index, Spire::Doc::IDocumentObject *entity) method.
  • Append a BookmarkEnd object at the end of the paragraph using Paragraph->AppendBookmarkEnd(LPCWSTR_S name) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

	//Create a Document object
	Document* document = new Document();

	//Load a Word file
	document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample2.docx");

	//Get a specific paragraph
	Paragraph* paragraph = document->GetSections()->GetItem(0)->GetParagraphs()->GetItem(1);

	//Create a bookmark start
	BookmarkStart* start = new BookmarkStart(document, L"myBookmark");

	//Insert the bookmark start before the selected text
	paragraph->GetChildObjects()->Insert(0, start);

	//Append a bookmark end at the end of the paragraph
	paragraph->AppendBookmarkEnd(L"myBookmark");

	//Save the document to another Word file
	document->SaveToFile(L"Output/AddBookmarkToParagraph.docx", FileFormat::Docx2013);
	document->Close();
	delete document;
}

C++: Add or Remove Bookmarks in Word Documents

Add a Bookmark to Selected Text in C++

A bookmark can also be inserted to a specific position within a paragraph. The following are the steps to add a bookmark to selected text using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Find the selected text from the document using Document->FindAllString() method.
  • Find the owner paragraph of the text using TextSelection-> ->GetAsOneRange()->GetOwnerParagraph() method.
  • Get the index of the text in the paragraph using Paragraph->GetChildObjects()->IndexOf() method.
  • Create a BookmarkStart object, and insert it before the selected text using Paragraph->GetChildObjects()->Insert(int index, Spire::Doc::IDocumentObject *entity) method.
  • Create a BookmarkEnd object, and insert it after the selected text using Paragraph->GetChildObjects()->Insert(int index, Spire::Doc::IDocumentObject *entity) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

	//Create a Document object
	Document* document = new Document();

	//Load a Word file
	document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample2.docx");

    //Specify text to find
    wstring stringToFind = L"business-to-consumer";

    //Find the text from the document
    vector<TextSelection*>  finds = document->FindAllString(stringToFind.c_str(), false, true);
    TextSelection* specificText = finds[0];

    //Find the paragraph where the text is located
    Paragraph* para = specificText->GetAsOneRange()->GetOwnerParagraph();

    //Get the index of the text in the paragraph
    int index = para->GetChildObjects()->IndexOf(specificText->GetAsOneRange());

    //Create a bookmark start
    BookmarkStart* start = new BookmarkStart(document, L"myBookmark");

    //Insert the bookmark start before the selected text
    para->GetChildObjects()->Insert(index, start);

    //Create a bookmark end
    BookmarkEnd* end = new BookmarkEnd(document, L"myBookmark");

    //Insert the bookmark end after the selected text
    para->GetChildObjects()->Insert(index + 2, end);

	//Save the document to another Word file
	document->SaveToFile(L"Output/AddBookmarkToText.docx", FileFormat::Docx2013);
	document->Close();
	delete document;
}

C++: Add or Remove Bookmarks in Word Documents

Remove Bookmarks from a Word Document in C++

Using Spire.Doc for C++, you can easily get and remove all bookmarks or a particular bookmark from a Word document. The following are the detailed steps.

  • Create a Document object.
  • Load a Word file containing bookmarks using Document->LoadFromFile() method.
  • Get a specific bookmark using Document->GetBookmarks()->GetItem() method.
  • Remove the bookmark using Document->GetBookmarks()->Remove() method. To remove all bookmarks at once, use Document->GetBookmarks()->Clear() method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

	//Create a Document object
	Document* document = new Document();

	//Load a Word file
	document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\Bookmarks.docx");

	//Get a specific bookmark by its index
	Bookmark* bookmark = document->GetBookmarks()->GetItem(0);

	//Remove the bookmark
	document->GetBookmarks()->Remove(bookmark);

	//Remove all bookmarks
	//document->GetBookmarks()->Clear();

	//Save the document to another Word file
	document->SaveToFile(L"Output/RemoveBookmarks.docx", FileFormat::Docx2013);
	document->Close();
	delete document;
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.