Spire.Cloud.PDF offer the pdfPathApi interface to draw shapes in a PDF document. This tutorial will showcase how to draw a line and a rectangle in a PDF document separately.
Draw a Line
import spire.cloud.pdf.sdk.ApiException;
import spire.cloud.pdf.sdk.Configuration;
import spire.cloud.pdf.sdk.api.PdfPathApi;
public class DrawLine {
private static String appId = "App ID";
private static String appKey = "App Key";
private static String baseUrl = "https://api.cloudxdocs.com";
public static void main(String[] args) throws ApiException {
//Create a Configuration object based on your App ID and App Key
Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create a PdfPathApi instance
PdfPathApi pdfPathApi = new PdfPathApi(configuration);
//Load a pdf sample
String name = "Sample.pdf";
//Specify the password used to open the sample, and it's null if nothing
String password = null;
//Use the 2G storage provided by E-iceblue Cloud, and it’s null by default
String storage = null;
//Specify the folder storing the sample, and it’s null if nothing
String folder = "input";
//Specify the storage path of the resulting document
String outPath = "output/DrawLine.pdf";
//Specify the page where a line are drawn
int pageNumber = 1;
//Set the x and y coordinate
float firstPointfX = 100;
float firstPointfY = 250;
//Set the length and inclination of the line
float secondPointfX = 350;
float secondPointfY = 270;
//Call the drawLine method to draw a line in a PDF document
pdfPathApi.drawLine(name, outPath, pageNumber, firstPointfX, firstPointfY, secondPointfX, secondPointfY, folder, storage, password);
}
}
Output
Draw a Rectangle
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 DrawRectangle {
private static String appId = "App ID";
private static String appKey = "App Key";
private static String baseUrl = "https://api.cloudxdocs.com";
public static void main(String[] args) throws ApiException {
//Create a Configuration object based on your App ID and App Key
Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create a pdfPathApi instance
PdfPathApi pdfPathApi = new PdfPathApi(configuration);
//Load a pdf sample
String name = "Sample.pdf";
//Specify the password used to open the sample, and it's null if nothing
String password = null;
//Use the 2G storage provided by E-iceblue Cloud, and it’s null by default
String storage = null;
//Specify the folder storing the sample, and it’s null if nothing
String folder = "input";
//Specify the storage path of the resulting document
String outPath = "output/DrawRectangle.pdf";
//Specify the page where a line are drawn
int pageNumber = 1;
//Set the x and y coordinate
RectangleF rect = new RectangleF();
rect.setX(100f);
rect.setY(150f);
//Set the size of the rectangle
rect.setWidth(300f);
rect.setHeight(60f);
//Call the drawRectanglef method to draw a rectangle in a PDF document
pdfPathApi.drawRectanglef(name, outPath, pageNumber, rect, folder, storage, password);
}
}
Output