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); } }
矩形繪制效果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/144235.html
標籤:Java
上一篇:easypoi添加下拉預選值
