Spire.Cloud.PDF API提供了PdfAttachmentsApi接口支持添加附件addAttachment()、下载附件downloadAttachment()、获取附件信息getAttachmentsInfo()等,本文将通过Java代码示例介绍具体实现方法。详细内容参考以下步骤:
一、导入jar
创建maven程序,并通过maven仓库安装Spire.Cloud.SDK.jar包及其依赖。详细步骤可参考这篇文章。
二、登录冰蓝云账号,创建文件夹,上传文档
三、创建应用程序,获取App ID及App Key
四、Java代码示例
1、添加附件
import spire.cloud.pdf.sdk.ApiException;
import spire.cloud.pdf.sdk.Configuration;
import spire.cloud.pdf.sdk.api.PdfAttachmentsApi;
import java.io.File;
public class AddAttachment {
//配置账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl= "https://api.e-iceblue.cn";
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
static PdfAttachmentsApi pdfAttachmentsApi = new PdfAttachmentsApi(configuration);
public static void main(String[] args) throws ApiException {
String name = "sample.pdf";//用于测试的PDF源文档
String outPath = "output/AddAttachment.pdf";//结果文档路径(结果文档保存在云端output文件夹下)
File file = new File("test.docx");//加载附件文档
String attachmentFileName = "AddAttachment.docx";//设置附件文档名称
String attachmentDescription = "A Nice File";//附件描述
String folder = "input";//源文档所在文件夹
String storage = null;//冰蓝云提供的2G免费云存储空间
String password = null;//源文档密码
//调用方法添加附件
pdfAttachmentsApi.addAttachment(name, outPath, file, attachmentFileName, attachmentDescription, folder, storage, password);
}
}
附件添加结果:
2. 下载附件文档(以上文中生成的PDF文档为例,读取添加的Word文档附件)
import spire.cloud.pdf.sdk.ApiException;
import spire.cloud.pdf.sdk.Configuration;
import spire.cloud.pdf.sdk.api.PdfAttachmentsApi;
import java.io.File;
public class DownloadAttachment {
//配置账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl= "https://api.e-iceblue.cn";
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
static PdfAttachmentsApi pdfAttachmentsApi = new PdfAttachmentsApi(configuration);
public static void main(String[] args) throws ApiException {
String name = "AddAttachment.pdf";//包含附件的PDF源文档
Integer orderNumber = 1;
String folder = "output";//源文档所在云端文件夹
String storage = null;//冰蓝云提供的2G免费云存储空间
String password = null;//源文档密码
//调用方法下载PDF文档中的附件文档
File response = pdfAttachmentsApi.downloadAttachment(name, orderNumber, folder, storage, password);
System.out.println(response);
}
}
读取结果如下,可在路径中查看下载的附件文档:
3、获取附件信息
import spire.cloud.pdf.sdk.ApiException;
import spire.cloud.pdf.sdk.Configuration;
import spire.cloud.pdf.sdk.api.PdfAttachmentsApi;
import spire.cloud.pdf.sdk.model.Attachments;
public class GetAttachmentInfo {
//配置账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl= "https://api.e-iceblue.cn";
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
static PdfAttachmentsApi pdfAttachmentsApi = new PdfAttachmentsApi(configuration);
public static void main(String[] args) throws ApiException {
String name = "AddAttachment.pdf";//包含附件的PDF源文档
String folder = "output";//源文档所在文件夹
String storage = null;//冰蓝云提供的2G云存储空间
String password = null;//源文档密码
//调用方法获取附件信息
Attachments response = pdfAttachmentsApi.getAttachmentsInfo(name, folder, storage, password);
System.out.println(response);
}
}
信息读取结果: