Spire.Cloud.SDK for Java提供了PdfAttachmentsApi介面添加附件addAttachment()、下載附件downloadAttachment()、獲取附件資訊getAttachmentsInfo(),本文將通過Java代碼示例介紹具體實作方法,詳細內容參考以下步驟:
一、匯入jar檔案,(有2種方式)
(推薦)方式1. 創建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,可參考這里的匯入方法,
匯入結果:

方式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); } }
附件資訊讀取結果:

(完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/150232.html
標籤:Java
