zoukankan      html  css  js  c++  java
  • Java 在PDF中绘制形状(基于Spire.Cloud.SDK for Java)

    Spire.Cloud.SDK for Java提供了pdfPathApi接口可用于在PDF文档中绘制形状(或图形),如绘制线条形状drawLine()、绘制矩形形状drawRectanglef(),下面将介绍如何通过Java示例和步骤来实现:

    一、导入jar文件。(有2种方式)

    创建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,可参考这里的导入方法。

    导入结果:

    二、登录冰蓝云账号,创建文件夹,上传文档

    三、创建应用程序,获取App ID及App Key

    四、Java代码示例

    【示例1】绘制线条形状

    import spire.cloud.pdf.sdk.ApiException;
    import spire.cloud.pdf.sdk.Configuration;
    import spire.cloud.pdf.sdk.api.PdfPathApi;
    
    public class DrawLine {
        //配置账号信息
        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 PdfPathApi pdfPathApi = new PdfPathApi(configuration);
    
        public static void main(String[] args) throws ApiException {
            String name = "samplefile.pdf";//用于测试的PDF源文档
            String outPath = "output/DrawLine.pdf";//结果文档路径(保存在冰蓝云端output文件夹下)
            int pageNumber = 1;//指定需要绘制线段的PDF页面
            float firstPointfX = 100;//指定线段起始点坐标
            float firstPointfY = 150;
            float secondPointfX = 400;
            float secondPointfY = 150;
            String folder = "input";//源文档输在文件夹
            String storage = null;//冰蓝云提供的2G免费云存储空间
            String password = null;//源文档密码(无密码设置为null)
    
            //调用方法绘制线条
            pdfPathApi.drawLine(name, outPath, pageNumber, firstPointfX, firstPointfY, secondPointfX, secondPointfY, folder, storage, password);
        }
    }

    线条绘制效果:

    【示例2】绘制矩形形状

    import spire.cloud.pdf.sdk.ApiException;
    import spire.cloud.pdf.sdk.Configuration;
    import spire.cloud.pdf.sdk.api.PdfPathApi;
    import spire.cloud.pdf.sdk.model.RectangleF;
    
    public class DrawRec {
        //配置账号信息
        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 PdfPathApi pdfPathApi = new PdfPathApi(configuration);
    
        public static void main(String[] args) throws ApiException {
            String name = "samplefile1.pdf";//加载需要添加形状的PDF源文档
            String outPath = "output/DrawRectanglef.pdf";//指定结果文档路径(保存在冰蓝云端output文件夹下)
            int pageNumber = 1;//指定需要添加形状的PDF页面
    
            RectangleF rect = new RectangleF();//创建RectangleF类的对象
            rect.setX(100f);//指定形状坐标
            rect.setY(100f);
            rect.setWidth(350f);//指定形状宽度、高度
            rect.setHeight(60f);
    
            String folder = "input";//源文档所在文件夹
            String storage = null;//冰蓝云提供的2G免费存储空间
            String password = null;//源文档密码(无密码设置为null)
    
            //调用方法绘制矩形
            pdfPathApi.drawRectanglef(name, outPath, pageNumber, rect, folder, storage, password);
        }
    }

    矩形绘制效果:

  • 相关阅读:
    洛谷P2569 (BZOJ1855)[SCOI2010]股票交易 【单调队列优化DP】
    洛谷 P2254 [NOI2005]瑰丽华尔兹(单调栈优化DP)
    CF372C Watching Fireworks is Fun(单调队列优化DP)
    2019牛客全国多校第八场A题 All-one Matrices(单调栈)
    HDU3896 Greatest TC(双联通分量+倍增)
    2019牛客多校第7场
    ZOJ 2112 Dynamic Rankings(树状数组+主席树)
    2019 杭电多校第六场 题解
    HDU2242 考研路茫茫——空调教室 (双联通分+树形DP)
    HDU5536 Chip Factory
  • 原文地址:https://www.cnblogs.com/Yesi/p/13292013.html
Copyright © 2011-2022 走看看