Create a Fillable PDF with Java REST API

This quick tutorial guides you on how to create a fillable PDF with Java REST API. You will learn to automatically create PDF fillable form with Java RESTful Service using the Java-based Cloud SDK. You will learn to set various properties and customize the form field before adding it to the PDF.

Prerequisite

Steps to Build a Fillable PDF with Java Low Code API

  1. Configure the PdfApi object by setting the client ID and secret to create the fillable PDF
  2. Create a field and set its parameters
  3. Create a rectangle to place the field on the PDF page and set to the field
  4. Create a list of selected items and set the field type
  5. Upload the source PDF file into the cloud storage by assigning a name for adding a field
  6. Call the PostCreateField() method to insert the field on the specified page
  7. Check the API response and save the resultant PDF file on the disk

These steps summarize how to create a fillable PDF with Java-based API. Create the PdfAp object, create a field, and set parameters such as default value, name, rectangle for size and position, and field type. Finally, load the source PDF file, add the PostCreateField on the defined page, and save the output PDF file.

Code to Generate Editable PDF with Java REST API

import com.aspose.pdf.cloud.sdk.ApiException;
import com.aspose.pdf.cloud.sdk.Configuration;
import com.aspose.pdf.cloud.sdk.api.PdfApi;
import com.aspose.pdf.cloud.sdk.model.AsposeResponse;
import com.aspose.pdf.cloud.sdk.model.Field;
import com.aspose.pdf.cloud.sdk.model.Rectangle;
import com.aspose.pdf.cloud.sdk.model.FieldType;
import com.aspose.pdf.cloud.sdk.model.FileUploadResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PdfTasks {
public static void createFormField() throws ApiException, IOException {
// Initialize API with credentials
String clientId = "ClientID";
String clientSecret = "ClientSecret";
// Setup the Aspose API configuration
Configuration configuration = new Configuration(clientId, clientSecret);
PdfApi pdfApi = new PdfApi(configuration);
String fileName = "sample.pdf"; // Input PDF file
Integer pageNo = 2; // The page number where the form field should be added
// Create the field
List<String> values = new ArrayList<>();
values.add("TestDataForPDF");
Field field = new Field();
field.setName("StudentName");
field.setValues(new ArrayList<String>() {{
add("NewFieldValue");
}});
// Set the field position
Rectangle rectangle = new Rectangle();
rectangle.setLLX(0.0f); // Lower-left X
rectangle.setLLY(0.0f); // Lower-left Y
rectangle.setURX(0.0f); // Upper-right X
rectangle.setURY(0.0f); // Upper-right Y
field.setRect(rectangle);
field.setSelectedItems(new ArrayList<Integer>() {{
add(1);
}});
field.setType(FieldType.Text);
try {
// Upload the source file to Aspose Cloud storage
File pdfFile = new File(fileName);
try (FileInputStream fileInputStream = new FileInputStream(pdfFile)) {
FileUploadResponse uploadResult = pdfApi.uploadFile(fileName, fileInputStream);
System.out.println("File uploaded: " + uploadResult.getStatus());
}
// Create form field in the PDF
AsposeResponse apiResponse = pdfApi.postCreateField(fileName, pageNo, field);
if (apiResponse != null && "OK".equals(apiResponse.getStatus())) {
// Download the created PDF file
FileOutputStream fileOutputStream = new FileOutputStream("output.pdf");
try {
fileOutputStream.write(pdfApi.downloadFile(fileName).readAllBytes());
System.out.println("Output file created: output.pdf");
} finally {
fileOutputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
createFormField();
} catch (ApiException | IOException e) {
e.printStackTrace();
}
}
}

This code demonstrates how to create typeable PDF with Java Low Code API. You can add the edit box by setting the FieldType property to Text or other types if a different control is to be added to the form. You can add a link to the field by using the Link class object and setting it as a property in the field.

This article has guided us to generate fillable PDF with Java REST Interface. If you want to convert PDF to Word document, refer to the article on How to Convert PDF to DOCX with Java REST API.

 English