This article entails the process to add bookmark in Word with NET REST API. We will use Aspose.Words for .NET Cloud SDK to add bookmark to Word document with C# Low Code API. You will set various bookmark parameters and invoke API calls to add this bookmark to your Word file on the local disk.
Prerequisite
- Create an account and get API credentials
- Download Aspose.Words Cloud SDK for Dotnet to add a bookmark in a Word file
- Setup C# solution project with the above SDK
Steps to Create Bookmark in Word with NET REST API
- Set Client ID and Client Secret for the API to add a bookmark
- Instantiate an object of the WordsApi class with your client credentials
- Read the source Word file into the memory stream for creating a bookmark
- Define the start and end range of the bookmark using the PositionInsideNode class
- Create the InsertBookmarkOnlineRequest object by providing the required parameters
- Call the InsertBookmarkOnline method to insert a bookmark according to the InsertBookmarkOnlineRequest
- Save the resultant Word file with the new bookmark
The aforementioned steps describe how to create a bookmark in Word with C# Low Code API. Commence the process by creating a WordsApi object followed by reading the source Word file and defining the bookmark parameters. Finally, add this bookmark to your Word file by calling a few API calls followed by saving the resultant Word file on the local disk.
Code to Insert a Bookmark in Word with C# REST API
using System; | |
using System.IO; | |
using Aspose.Words.Cloud.Sdk; | |
using Aspose.Words.Cloud.Sdk.Model; | |
using Aspose.Words.Cloud.Sdk.Model.Requests; | |
namespace WordsSample.Words | |
{ | |
public class Bookmarks | |
{ | |
public void AddBookmark() | |
{ | |
try | |
{ | |
// Set the client secret and ID | |
var config = new Configuration(); | |
config.ClientSecret = "Client Secret"; | |
config.ClientId = "Client ID"; | |
// Instantiate the WordsApi object | |
var wordsApi = new WordsApi(config); | |
// Read the input file | |
using var requestDocument = File.OpenRead("Sample.docx"); | |
// Define start and end range for the bookmark | |
var requestBookmarkStartRange = new PositionInsideNode | |
{ | |
NodeId = "0.0.0.0", | |
Offset = 0 | |
}; | |
var requestBookmarkEndRange = new PositionInsideNode() | |
{ | |
NodeId = "0.0.0.0", | |
Offset = 0 | |
}; | |
// Initialize the BookmarkInsert object | |
var requestBookmark = new BookmarkInsert() | |
{ | |
StartRange = requestBookmarkStartRange, | |
EndRange = requestBookmarkEndRange, | |
Name = "new_bookmark", | |
Text = "Some text" | |
}; | |
string output = "output.docx"; | |
// Insert the bookmark | |
var insertRequest = new InsertBookmarkOnlineRequest(requestDocument, requestBookmark,destFileName:output); | |
var insertTask = wordsApi.InsertBookmarkOnline(insertRequest); | |
insertTask.Wait(); | |
var result = insertTask.Result; | |
// Save the Word file with the bookmark | |
if (result.Document.TryGetValue(output, out var stream)) | |
{ | |
stream.Position = 0; | |
using (var fileStream = File.Create(output)) | |
{ | |
stream.Seek(0, SeekOrigin.Begin); | |
stream.CopyTo(fileStream); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} |
This sample code exhibits how to add bookmark in Word with C# REST API. The PositionInsideNode class is used to set the start and end range of the bookmark in the destination Word file. The resultant stream is returned as a dictionary item with the specified key and can be retrieved using the TryGetValue method as demonstrated in the sample code.
In this topic, we have learned how to make a bookmark in Word with C# REST API. If you are looking to convert a Word file to an HTML file, refer to the article on Convert DOCX to HTML with NET REST API.