Spire.Cloud.SDK for Java WebAPI提供了pdfBookmarkApi接口可用于添加书签addBookmark()、更新书签updateBookmark()、获取书签信息getBookmarksInfo()、以及删除书签deleteBookmarks(),下面将通过具体操作步骤和Java代码示例来展示如何实现以上操作要求。
一、导入jar文件
创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在pom.xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:
com.e-iceblue
cloud
http://repo.e-iceblue.cn/repository/maven-public/
cloud
spire.cloud.sdk
3.5.0
com.google.code.gson
gson
2.8.1
com.squareup.okhttp
logging-interceptor
2.7.5
com.squareup.okhttp
okhttp
2.7.5
com.squareup.okio
okio
1.6.0
io.gsonfire
gson-fire
1.8.0
io.swagger
swagger-annotations
1.5.18
org.threeten
threetenbp
1.3.5
完成配置后,点击“Import Changes” 即可导入所有需要的jar文件。如果使用的是Eclipse,可参考这里的导入方法。
导入结果:
二、登录冰蓝云账号,创建文件夹,上传文档
三、创建应用程序,获取App ID及App Key
四、Java代码示例
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";//源文档所在的云端文件夹
String password = null;//源文档密码(无密码设置为null)
String storage = null;//冰蓝云提供的2G免费存储空间
//调用方法添加书签到PDF
pdfBookmarkApi.addBookmark(name, outPath, bookmarkPath, bookmark, folder, storage, password);
}
}
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);
}
}
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);*/
}
}
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);*/
}
}
(完)