Spire.Cloud.PDF API provides developers with the PdfImageApi interface to add images to PDF document and extract images from PDF document. This article will demonstrate how to add an image to a PDF document by using Spire.Cloud.PDF API.
import spire.cloud.pdf.sdk.*;
import java.io.File;
import spire.cloud.pdf.sdk.api.PdfImagesApi;
public class AddImagetoPDF {
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 App ID, App Key and base Url
Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create an instance of PdfImageApi
PdfImagesApi api = new PdfImagesApi(configuration);
//Specify the name of the input PDF document
String name = "E-iceblue.pdf";
//Specify the storage path and name of the generated document
String destFilePath = "output/AddImage.pdf";
//Specify the folder where the input PDF document is stored, set it to null if there's none
String folder = "input";
//Specify the original password of the input document, set it to null if there's none
String password = null;
//Store the document in the 2G default storage space offered by E-iceblue Cloud
String storage = null;
//The index of the page number where the image needs to be added
int pageNumber = 1;
//Load an image and set its position
File file = new File("C:\\Users\\Administrator\\Desktop\\logo.jpg");
Float x = 0f;
Float y = 0f;
//Set the width and height of the image
Float width = 80f;
Float height = 80f;
//Add an image to PDF documents with the "addImage" method
api.addImage(name, destFilePath, pageNumber, file, x, y, width, height, folder,storage, password);
}
}
Open the generated document in Spire.Cloud Online Editor: