How to Remove PDF Password with Java
This tutorial shows you how to remove PDF password from a Java application with the Encrypt PDF API Tool. The request supplies the authorized current open password to /decrypted-pdf and receives an unencrypted PDF resource for subsequent authorized downstream processing steps. The walkthrough gives you a practical starting point that you can adapt when you need to remove PDF password in your own document workflow.
Why Remove PDF Password with Java?
A controlled ingestion service may receive password-protected PDFs together with authorized credentials. Decrypting them allows subsequent OCR, conversion, extraction, or archival processing.
The Java request supplies the encrypted PDF and current_open_password to /decrypted-pdf. The password must be known; this operation does not recover or bypass unknown credentials.
Java Code Example
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import org.json.JSONObject;
public class DecryptedPDF {
// By default, we use the US-based API service. This is the primary endpoint for global use.
private static final String API_URL = "https://api.pdfrest.com";
// For GDPR compliance and enhanced performance for European users, you can switch to the EU-based
// service by commenting out the URL above and uncommenting the URL below.
// For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
// private static final String API_URL = "https://eu-api.pdfrest.com";
// Specify the path to your file here, or as the first argument when running the program.
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf";
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
// You can also put the environment variable in a .env file.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
public static void main(String[] args) {
final boolean DELETE_SENSITIVE_FILES = false; // toggle deletion (default: false)
File inputFile;
if (args.length > 0) {
inputFile = new File(args[0]);
} else {
inputFile = new File(DEFAULT_FILE_PATH);
}
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
final RequestBody inputFileRequestBody =
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("current_open_password", "password")
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/decrypted-pdf")
.post(requestBody)
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
Response response = client.newCall(request).execute();
System.out.println("Result code " + response.code());
if (response.body() != null) {
String respStr = response.body().string();
System.out.println(prettyJson(respStr));
// All files uploaded or generated are automatically deleted based on the
// File Retention Period as shown on https://pdfrest.com/pricing.
// For immediate deletion of files, particularly when sensitive data
// is involved, an explicit delete call can be made to the API.
//
// Deletes all files in the workflow, including outputs. Save all desired files before
// enabling this step.
if (DELETE_SENSITIVE_FILES) {
org.json.JSONObject parsed = new org.json.JSONObject(respStr);
String outputId = parsed.getString("outputId");
String inputId = parsed.getString("inputId");
String deleteJson = String.format("{ \"ids\":\"%s, %s\" }", inputId, outputId);
RequestBody deleteBody =
RequestBody.create(deleteJson, MediaType.parse("application/json"));
Request deleteRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/delete")
.post(deleteBody)
.build();
try (Response deleteResp =
new OkHttpClient()
.newBuilder()
.readTimeout(60, TimeUnit.SECONDS)
.build()
.newCall(deleteRequest)
.execute()) {
if (deleteResp.body() != null) {
System.out.println(prettyJson(deleteResp.body().string()));
}
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static String prettyJson(String json) {
// https://stackoverflow.com/a/9583835/11996393
return new JSONObject(json).toString(4);
}
}
Source: Java sample on GitHub.
Breaking Down the Code
Load the Java HTTP and JSON dependencies
import io.github.cdimascio.dotenv.Dotenv; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import okhttp3.*; import org.json.JSONObject;
This dependency set brings together the HTTP client, environment-based API key loading, and JSON utilities used later in the example.
Choose the input and load credentials
// service by commenting out the URL above and uncommenting the URL below.
// For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
// private static final String API_URL = "https://eu-api.pdfrest.com";
// Specify the path to your file here, or as the first argument when running the program.
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf";
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
// You can also put the environment variable in a .env file.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
public static void main(String[] args) {
final boolean DELETE_SENSITIVE_FILES = false; // toggle deletion (default: false)
File inputFile;
if (args.length > 0) {
inputFile = new File(args[0]);
} else {
inputFile = new File(DEFAULT_FILE_PATH);
}
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
The startup code shows where the input comes from and keeps the credential outside the request-building logic.
Build the multipart request body
final RequestBody inputFileRequestBody =
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("current_open_password", "password")
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/decrypted-pdf")
.post(requestBody)
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
The Java request supplies the encrypted PDF and current_open_password to /decrypted-pdf. The password must be known; this operation does not recover or bypass unknown credentials. The multipart section sets MultipartBody.FORM, allowing OkHttp to generate the matching boundary automatically.
Send the request to pdfRest
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/decrypted-pdf")
.post(requestBody)
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
Response response = client.newCall(request).execute();
The request builder reads the credential through dotenv and targets the endpoint shown in the source, with the EU hostname documented beside the default US value.
Read the API response
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
Response response = client.newCall(request).execute();
System.out.println("Result code " + response.code());
if (response.body() != null) {
String respStr = response.body().string();
System.out.println(prettyJson(respStr));
// All files uploaded or generated are automatically deleted based on the
// File Retention Period as shown on https://pdfrest.com/pricing.
// For immediate deletion of files, particularly when sensitive data
// is involved, an explicit delete call can be made to the API.
//
// Deletes all files in the workflow, including outputs. Save all desired files before
// enabling this step.
The response block checks the server result before treating the body as output and exposes diagnostic JSON when the operation cannot be completed.
Beyond the Tutorial
You have now seen how to remove PDF password from a Java application. With that foundation, you can use the decrypted output only within authorized workflows, and apply deletion or re-encryption policies appropriate for sensitive material.
When you move beyond the sample, treat the current password as a secret: do not place it in logs, URLs, or persistent source code. Limit access to the decrypted resource, remove temporary copies promptly, and apply any required encryption or permissions policy before further distribution.
You can experiment with the Remove PDF Password operation in API Lab. The Encrypt PDF API Tool documentation describes the full parameter and response contract.
Note: The Remove PDF Password example above uses multipart form data; to avoid transferring the same input again, see the request shape in the JSON payload example.