Java 添加、更新、获取、删除PDF中的书签
2021-04-25 04:29
标签:update 方法 top art webapi folder eclipse model 仓库 完成配置后,点击“Import Changes” 即可导入所有需要的jar文件。如果使用的是Eclipse,可参考这里的导入方法。 书签添加结果: 书签更新结果: 书签信息读取结果: 书签删除结果: (完) Java 添加、更新、获取、删除PDF中的书签 标签:update 方法 top art webapi folder eclipse model 仓库 原文地址:https://blog.51cto.com/eiceblue/2509114
一、导入jar文件
创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在pom.xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:
导入结果:
二、登录冰蓝云账号,创建文件夹,上传文档
三、创建应用程序,获取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);*/
}
}
上一篇:Python爬虫 — 百度翻译
下一篇:Java日期和时间
文章标题:Java 添加、更新、获取、删除PDF中的书签
文章链接:http://soscw.com/index.php/essay/79254.html