Spire.Cloud.PDF offers an interface called PdfPropertiesApi to add, update or delete document properties in PDF. This tutorial will demonstrate how to use the interface to do it in Java.
Add/Update Document Properties
import spire.cloud.pdf.sdk.ApiException;
import spire.cloud.pdf.sdk.Configuration;
import spire.cloud.pdf.sdk.api.PdfPropertiesApi;
import spire.cloud.pdf.sdk.model.DocumentProperties;
import spire.cloud.pdf.sdk.model.DocumentProperty;
import java.util.ArrayList;
public class AddDocumentProperties {
static String appId = "App ID";
static String appKey = "App Key";
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 PdfConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a PdfPropertiesApi instance
PdfPropertiesApi pdfPropertiesApi = new PdfPropertiesApi(PdfConfiguration);
//Load a PDF sample
String name = "Sample.pdf";
//Specify the folder storing the sample, and it’s null if nothing
String folder = "input";
//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 storage path of the resulting document
String outPath = "output/AddProperties.pdf";
DocumentProperties properties = new DocumentProperties();
properties.setList(new ArrayList());
//Create built-in document properties
DocumentProperty property = new DocumentProperty();
property.setName("author");
property.setValue("Jack Prelutsky");
properties.getList().add(property);
property = new DocumentProperty();
property.setName("creator");
property.setValue("Tina");
properties.getList().add(property);
property = new DocumentProperty();
property.setName("keywords");
property.setValue("Nose");
properties.getList().add(property);
property = new DocumentProperty();
property.setName("producer");
property.setValue("Daniel");
properties.getList().add(property);
property = new DocumentProperty();
property.setName("subject");
property.setValue("A English Poem");
properties.getList().add(property);
property = new DocumentProperty();
property.setName("title");
property.setValue("Be Glad Your Nose is on Your Face");
properties.getList().add(property);
//Create customized document properties
property = new DocumentProperty();
property.setName("Time");
property.setValue("April 21st 2021");
properties.getList().add(property);
property = new DocumentProperty();
property.setName("Place");
property.setValue("China");
properties.getList().add(property);
//Call the addOrUpdateProperties method to add or update document properties for the PDF sample
pdfPropertiesApi.addOrUpdateProperties(name, outPath, properties, folder, storage, password);
}
}
Output
Delete Document Properties
import spire.cloud.pdf.sdk.ApiException;
import spire.cloud.pdf.sdk.Configuration;
import spire.cloud.pdf.sdk.api.PdfPropertiesApi;
public class DeleteDocumentProperties {
static String appId = "App ID";
static String appKey = "App Key";
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 PdfConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a PdfPropertiesApi instance
PdfPropertiesApi pdfPropertiesApi = new PdfPropertiesApi(PdfConfiguration);
//Load a PDF sample
String name = "AddProperties.pdf";
//Specify the folder storing the sample, and it’s null if nothing
String folder = "input";
//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 storage path of the resulting document
String outPath = "output/DeleteProperties.pdf";
//Call the deleteProperties method to delete document properties of the PDF sample
pdfPropertiesApi.deleteProperties(name, outPath, folder, storage, password);
}
}