Add Bookmarks to PDF in Node.js SDK
Add Bookmarks to a PDF Document using Cloud Node.js SDK
Get StartedHow to add Bookmarks via Node.js SDK
In order to add Bookmarks to PDF, we’ll use Aspose.PDF Cloud Node.js SDK. This Cloud SDK assists Node.js programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Node.js programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud Node.js SDK.
Package Manager Console Command
npm install asposepdfcloud --save
Steps to add Bookmarks via Cloud Node.js
Aspose.PDF Cloud developers can easily add Bookmarks to PDF in just a few lines of code.
- Create an object to connect to the Pdf.Cloud API
- Upload your document file
- Create a new Bookmarks with the required properties
- Append new Bookmarks to the document using postBookmark() function
- Perform some action after successful addition
- Download the result if needed it
This sample code shows adding Bookmarks in PDF documents using Node.js
import credentials from "./credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
import fs from 'node:fs/promises';
import path from 'node:path';
import { PdfApi } from "asposepdfcloud";
import { Color } from "asposepdfcloud/src/models/color.js";
import { Link } from "asposepdfcloud/src/models/link.js";
import { Bookmark } from "asposepdfcloud/src/models/bookmark.js";
import { Bookmarks } from "asposepdfcloud/src/models/bookmarks.js";
const configParams = {
LOCAL_FOLDER: "C:\\Samples\\",
PDF_DOCUMENT_NAME: "sample.pdf",
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
NEW_BOOKMARK_TITLE: "• Productivity improvement",
PARENT_BOOKMARK_FOR_APPEND: "", //The parent bookmark path. Specify an empty string when adding a bookmark to the root.
NEW_BOOKMARK_PAGE_NUMBER: 2,
};
const pdfApi = new PdfApi(credentials.id, credentials.key);
const pdfBookmarks = {
async uploadDocument() {
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
const pdfFileData = await fs.readFile(pdfFilePath);
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
},
async downloadResult() {
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
await fs.writeFile(filePath, changedPdfData.body);
console.log("Downloaded: " + filePath);
},
async appendBookmarkLink() {
const bookmarkLink = new Link({ rel: "self" });
const bookmarkColor = new Color({ a: 255, r: 0, g: 255, b: 0 });
const newBookmark = new Bookmark();
newBookmark.title = configParams.NEW_BOOKMARK_TITLE;
newBookmark.italic = true;
newBookmark.bold = false;
newBookmark.links = [bookmarkLink];
newBookmark.color = bookmarkColor;
newBookmark.action = "GoTo";
newBookmark.level = 1;
newBookmark.pageDisplayLeft = 83;
newBookmark.pageDisplayTop = 751;
newBookmark.pageDisplayZoom = 2;
newBookmark.pageNumber = configParams.NEW_BOOKMARK_PAGE_NUMBER;
const response = await pdfApi.postBookmark(configParams.PDF_DOCUMENT_NAME, configParams.PARENT_BOOKMARK_FOR_APPEND, [newBookmark]);
const { code, bookmarks } = response.body;
if (code === 200 && bookmarks) {
const addedBookmark = bookmarks.list[bookmarks.list.length - 1];
console.log("Appended bookmark: " + addedBookmark.links[0].href + " => " + addedBookmark.title);
return addedBookmark;
}
},
};
async function main() {
try {
await pdfBookmarks.uploadDocument();
await pdfBookmarks.appendBookmarkLink();
await pdfBookmarks.downloadResult();
} catch (error) {
console.error("Error:", error.message);
}
}
Work with the Bookmarks via Node.js SDK
Adding bookmarks to a PDF significantly improves document usability by enhancing navigation, organization, and accessibility. It is essential for large documents, ensuring users can efficiently find and access relevant content. Add the Bookmarks into PDF documents with Aspose.PDF Cloud Node.js SDK.
With our Node.js library you can
- Add PDF document’s header & footer in text or image format.
- Add tables & stamps (text or image) to PDF documents.
- Append multiple PDF documents to an existing file.
- Work with PDF attachments, annotations, & form fields.
- Apply encryption or decryption to PDF documents & set a password.
- Delete all stamps & tables from a page or entire PDF document.
- Delete a specific stamp or table from the PDF document by its ID.
- Replace single or multiple instances of text on a PDF page or from the entire document.
- Extensive support for converting PDF documents to various other file formats.
- Extract various elements of PDF files & make PDF documents optimized.
- You can try out our free Apps to test the functionality online.
- Learning Resources
- Documentation
- Source Code
- API References
- Product Support
- Free Support
- Paid Support
- Blog
- Why Aspose.PDF Cloud for Node.js?
- Customers List
- Security