Spire.Cloud.SDK for Java WebAPI提供了pdfBookmarkApi接口可用于添加书签addBookmark()、更新书签updateBookmark()、获取书签信息getBookmarksInfo()、以及删除书签deleteBookmarks(),下面将通过具体操作步骤和Java代码示例来展示如何实现以上操作要求。
一、导入jar文件
创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在pom.xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>cloud</name>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> cloud </groupId>
<artifactId>spire.cloud.sdk</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId> com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId> com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId> com.squareup.okhttp </groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId> com.squareup.okio </groupId>
<artifactId>okio</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId> io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId> org.threeten </groupId>
<artifactId>threetenbp</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
完成配置后,点击“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); } }
书签添加结果:
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);*/ } }
书签删除结果:
(完)