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