zoukankan      html  css  js  c++  java
  • 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的依赖,如下:

    <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);
        }
    }

    附件信息读取结果:

    (完)

  • 相关阅读:
    python学习之函数基础
    Java并发编程之支持并发的list集合你知道吗
    Java并发编程之CAS第三篇-CAS的缺点及解决办法
    Java并发编程之CAS二源码追根溯源
    Java并发编程之CAS第一篇-什么是CAS
    Java并发编程之验证volatile指令重排-理论篇
    【免费百度网盘不限速】爱奇艺万能联播 百度网盘不限速的方法
    Java并发编程之验证volatile不能保证原子性
    Java并发编程之验证volatile的可见性
    Java并发编程学习前期知识下篇
  • 原文地址:https://www.cnblogs.com/Yesi/p/13253601.html
Copyright © 2011-2022 走看看