Spire.Cloud.PDF提供了pdfBookmarkApi接口可用于添加书签addBookmark()、更新书签updateBookmark()、获取书签信息getBookmarksInfo()、以及删除书签deleteBookmarks(),下面将通过具体操作步骤和Java代码示例来展示如何实现以上操作要求。
步骤一:创建maven程序,并通过maven仓库安装Spire.Cloud.SDK.jar包及其依赖。详细导入方法可参考这篇文章。
步骤二:登录冰蓝云账号,创建文件夹,上传测试文档。如不需要创建文件夹,也可直接上传文档,此时文档保存在根目录下。
步骤三:创建应用程序,获取App ID及App Key
步骤四:Java代码示例——调用Spire.Cloud.PDF API接口操作PDF中的书签
1、添加书签
import spire.cloud.pdf.sdk.*;
import spire.cloud.pdf.sdk.api.PdfBookmarksApi;
import spire.cloud.pdf.sdk.model.*;
public class AddBookmark {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);
static PdfBookmarksApi pdfBookmarkApi = new PdfBookmarksApi(PdfConfiguration);
public static void main(String[] args) throws ApiException{
String name = "sample.pdf";//需要添加书签的PDF源文档
String outPath = "output/AddBookmark.pdf";//结果文档路径
String bookmarkPath = "1";//指定书签级别
//创建书签,并设置书签标题、颜色、字体等等
Bookmark bookmark = new Bookmark();
bookmark.setTitle("testTitle");
Color color= new Color();
color.A(0);
color.R(255);
color.G(0);
color.B(0);
bookmark.setColor(color);
bookmark.setPageNumber(1);//添加书签的页面
bookmark.setBold(true);
bookmark.setItalic(true);
bookmark.setPageDisplay(Bookmark.PageDisplayEnum.XYZ);
bookmark.setPageDisplayLeft(10f);
bookmark.setPageDisplayTop(20f);
String folder = "input";//源文档所在的云端文件夹(无文件夹时,设置为null)
String password = null;//源文档密码(无密码设置为null)
String storage = null;//冰蓝云提供的2G免费存储空间
//调用方法添加书签到PDF
pdfBookmarkApi.addBookmark(name, outPath, bookmarkPath, bookmark, folder, storage, password);
}
}
书签添加结果:
2、更新PDF中的指定书签
import spire.cloud.pdf.sdk.*;
import spire.cloud.pdf.sdk.api.PdfBookmarksApi;
import spire.cloud.pdf.sdk.model.*;
public class UpdateBookmark {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);
static PdfBookmarksApi pdfBookmarkApi = new PdfBookmarksApi(PdfConfiguration);
public static void main(String[] args) throws ApiException {
String name = "AddBookmark.pdf";//包含书签的PDF源文档
String outPath = "output/UpdateBookmark.pdf";//指定结果文档路径
String bookmarkPath = "1";//指定书签级别
//实例化Bookmark类的对象,设置新的书签内容及格式
Bookmark bookmark = new Bookmark();
bookmark.title("updateTitile");
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);
String folder = "output";//源文档所在文件夹
String storage = null;//冰蓝云提供的2G免费存储空间
String password = null;//源文档密码
//调用方法更新书签
pdfBookmarkApi.updateBookmark(name, outPath, bookmarkPath, bookmark, folder, storage, password);
}
}
书签更新结果:
3、获取书签信息
import spire.cloud.pdf.sdk.*;
import spire.cloud.pdf.sdk.api.PdfBookmarksApi;
import spire.cloud.pdf.sdk.model.*;
public class GetBookmarkInfo {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);
static PdfBookmarksApi pdfBookmarkApi = new PdfBookmarksApi(PdfConfiguration);
public static void main(String[] args) throws ApiException{
String name = "Bookmarkfile.pdf";//包含书签的PDF源文档
String folder = "input";//源文档所在文件夹
//String bookmarkPath = "1-1";//如果只需要获取指定书签信息,需要指定书签级别
String storage = null;//冰蓝云提供的2G免费云存储空间
String password = null;//源文档密码
//调用方法读取所有书签信息,包括父级书签、子书签等
Bookmarks response = pdfBookmarkApi.getBookmarksInfo(name, folder, storage, password);
System.out.println(response);
/*//获取指定级别的书签信息
Bookmark response =pdfBookmarkApi.getBookmarkInfo(name, bookmarkPath, folder, storage, password);*/
}
}
书签信息读取结果:
4、删除书签
import spire.cloud.pdf.sdk.*;
import spire.cloud.pdf.sdk.api.PdfBookmarksApi;
public class DeleteBookmark {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl);
static PdfBookmarksApi pdfBookmarkApi = new PdfBookmarksApi(PdfConfiguration);
public static void main(String[] args) throws ApiException{
String name = "AddBookmark.pdf";//PDF源文档
String outPath = "output/DeleteBookmark.pdf";//指定结果文档路径
//String bookmarkPath = "2-3";//如需删除子书签,需要指定书签级别
String folder = "output";//源文档所在文件夹
String storage = null;//冰蓝云提供的2G免费云存储空间
String password = null;//源文档密码
//调用方法删除所有书签
pdfBookmarkApi.deleteBookmarks(name, outPath, folder, storage, password);
/*//调用方法删除子书签
pdfBookmarkApi.deleteBookmark(name, outPath, bookmarkPath, folder, storage, password);*/
}
}
书签删除结果: