Spire.Cloud.SDK for Java提供了PdfAttachmentsApi接口添加附件addAttachment()、下载附件downloadAttachment()、获取附件信息getAttachmentsInfo(),本文将通过Java代码示例介绍具体实现方法。详细内容参考以下步骤:
一、导入jar文件。(有2种方式)
(推荐)方式1. 创建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,可参考这里的导入方法。
导入结果:
方式2:手动下载jar包,然后解压文件,手动导入jar,同时还需另行手动导入其他几个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); } }
附件信息读取结果:
(完)