Java 添加、下载、读取PDF附件信息(基于Spire.Cloud.SDK for Java)
Spire.Cloud.SDK for Java提供了PdfAttachmentsApi接口添加附件addAttachment()、下载附件downloadAttachment()、获取附件信息getAttachmentsInfo(),本文将通过Java代码示例介绍具体实现方法。详细内容参考以下步骤: 一、导入jar文件。(有2种方式)
(推荐)方式1. 创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在pom.xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:
完成配置后,点击“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 = \; static String appKey = \;
static String baseUrl= \;
static Configuration configuration = new Configuration(appId, appKey, baseUrl); static PdfAttachmentsApi pdfAttachmentsApi = new PdfAttachmentsApi(configuration);
public static void main(String[] args) throws ApiException { String name = \;//用于测试的PDF源文档
String outPath = \;//结果文档路径(结果文档保存在云端output文件夹下) File file = new File(\);//加载附件文档 String attachmentFileName = \;//设置附件文档名称 String attachmentDescription = \;//附件描述 String folder = \;//源文档所在文件夹 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 = \;
static String appKey = \;
static String baseUrl= \;
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
static PdfAttachmentsApi pdfAttachmentsApi = new PdfAttachmentsApi(configuration);
public static void main(String[] args) throws ApiException { String name = \;//包含附件的PDF源文档 Integer orderNumber = 1;
String folder = \;//源文档所在云端文件夹 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 = \;
static String appKey = \;
static String baseUrl= \;
static Configuration configuration = new Configuration(appId, appKey, baseUrl); static PdfAttachmentsApi pdfAttachmentsApi = new PdfAttachmentsApi(configuration);
public static void main(String[] args) throws ApiException { String name = \;//包含附件的PDF源文档 String folder = \;//源文档所在文件夹 String storage = null;//冰蓝云提供的2G云存储空间 String password = null;//源文档密码 //调用方法获取附件信息 Attachments response = pdfAttachmentsApi.getAttachmentsInfo(name, folder, storage,
password);
System.out.println(response); } }
信息读取结果:
(完)