Spire.Cloud.PDF offers the pdfBookmarkApi interface to manipulate bookmarks in PDF documents. In this tutorial, I’ll show you how to add a bookmark to a PDF document and then how to update, get and delete the bookmark using Java codes.
Add a bookmark to PDF
import spire.cloud.pdf.sdk.*;
import spire.cloud.pdf.sdk.api.PdfBookmarksApi;
import spire.cloud.pdf.sdk.model.*;
public class AddBookmark {
//Create a configuration object based on your App ID and App Key
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a PdfBookmarkApi instance
static PdfBookmarksApi pdfBookmarkApi = new PdfBookmarksApi(PdfConfiguration);
public static void main (String[]args) throws ApiException {
//load a PDF sample
String name = "Sample.pdf";
//Specify the folder storing the sample, and it’s null if nothing
String folder = "input";
//Specify the password used to open the sample, and it's null if nothing
String password = null;
//Use the 2G storage provided by E-iceblue Cloud, and it’s null by default
String storage = null;
//Save the resulting document
String outPath = "AddBookmark.pdf";
//Create a bookmark and set its title, color, font and so on
Bookmark bookmark = new Bookmark();
bookmark.setTitle("NewBookmark");
Color color = new Color();
color.A(0);
color.R(255);
color.G(0);
color.B(0);
bookmark.setColor(color);
bookmark.setPageNumber(2);//Specify the page which needs to add the bookmark
bookmark.setBold(true);
bookmark.setItalic(true);
bookmark.setPageDisplay(Bookmark.PageDisplayEnum.XYZ);
bookmark.setPageDisplayLeft(10f);
bookmark.setPageDisplayTop(20f);
String bookmarkPath = "1"; //Specify the bookmark level
//Call the addBookmark method to add a bookmark to a PDF document
pdfBookmarkApi.addBookmark(name, outPath, bookmarkPath, bookmark, folder, storage, password);
}
}
Output
Update the existing bookmark in PDF
import spire.cloud.pdf.sdk.*;
import spire.cloud.pdf.sdk.api.PdfBookmarksApi;
import spire.cloud.pdf.sdk.model.*;
public class UpdateBookmark {
//Create a configuration object based on your App ID and App Key
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a PdfBookmarkApi instance
static PdfBookmarksApi pdfBookmarkApi = new PdfBookmarksApi(PdfConfiguration);
public static void main(String[] args) throws ApiException {
//load a PDF sample
String name = "AddBookmark.pdf";
//Specify the folder storing the sample, and it’s null if nothing
String folder = "input";
//Use the 2G storage provided by E-iceblue Cloud, and it’s null by default
String storage = null;
//Specify the password used to open the sample, and it's null if nothing
String password = null;
//Save the resulting document
String outPath = "UpdateBookmark.pdf";
//Specify the bookmark level
String bookmarkPath = "1";
//Instantiate a Bookmark object, and set the content and format of the new bookmark
Bookmark bookmark = new Bookmark();
bookmark.title("updateBookmark");
Color color = new Color();
color.A(0);
color.R(0);
color.G(0);
color.B(255);
bookmark.setColor(color);
bookmark.pageNumber(1);
bookmark.bold(false);
bookmark.italic(false);
bookmark.pageDisplay(Bookmark.PageDisplayEnum.XYZ);
bookmark.pageDisplayLeft(10f);
bookmark.pageDisplayTop(20f);
//Call the updateBookmark method to replace the existing bookmark with a new one
pdfBookmarkApi.updateBookmark(name, outPath, bookmarkPath, bookmark, folder, storage, password);
}
}
Output
Get information of bookmarks in PDF
import spire.cloud.pdf.sdk.*;
import spire.cloud.pdf.sdk.api.PdfBookmarksApi;
import spire.cloud.pdf.sdk.model.*;
public class GetBookmark {
//Create a configuration object based on your App ID and App Key
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a PdfBookmarkApi instance
static PdfBookmarksApi pdfBookmarkApi = new PdfBookmarksApi(PdfConfiguration);
public static void main(String[] args) throws ApiException {
//load a PDF sample including bookmarks
String name = "UpdateBookmark.pdf";
//Specify the folder storing the sample, and it’s null if nothing
String folder = null;
//Specify the bookmark level if you want to get information of a specified bookmark
//String bookmarkPath = "1-1";
//Use the 2G storage provided by E-iceblue Cloud, and it’s null by default
String storage = null;
//Specify the password used to open the sample, and it's null if nothing
String password = null;
//Call the pdfBookmarkApi method to get information of all bookmarks including parent bookmarks and child bookmarks
Bookmarks response = pdfBookmarkApi.getBookmarksInfo(name, folder, storage, password);
System.out.println(response);
/*//Call the pdfBookmarkApi method to get information of a specified bookmark
Bookmark response =pdfBookmarkApi.getBookmarkInfo(name, bookmarkPath, folder, storage, password);*/
}
}
Output
Delete bookmarks in PDF
import spire.cloud.pdf.sdk.*;
import spire.cloud.pdf.sdk.api.PdfBookmarksApi;
public class DeleteBookmark {
//Create a configuration object based on your App ID and App Key
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a PdfBookmarkApi instance
static PdfBookmarksApi pdfBookmarkApi = new PdfBookmarksApi(PdfConfiguration);
public static void main(String[] args) throws ApiException {
//load a PDF sample
String name = "UpdateBookmark.pdf";
//Specify the folder storing the sample, and it’s null if nothing
String folder = "input";
//Use the 2G storage provided by E-iceblue Cloud, and it’s null by default
String storage = null;
//Specify the password of the sample, and it's null if nothing
String password = null;
//Save the resulting document
String outPath = "DeleteBookmark.pdf";
//Specify the bookmark level if you want to delete specified child bookmarks
//String bookmarkPath = "2-3";
//Call the deleteBookmarks method to delete all bookmarks
pdfBookmarkApi.deleteBookmarks(name, outPath, folder, storage, password);
/*//Call the deleteBookmark method to delete specified child bookmarks
pdfBookmarkApi.deleteBookmark(name, outPath, bookmarkPath, folder, storage, password);*/
}
}