This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/java/extraction/open-password-protected-pdf.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Opening password-protected PDFs | Nutrient Java SDK

Some PDFs are encrypted and stay closed until the correct password unlocks them. An automated pipeline has no one to prompt, so the password has to travel with the open request. Once Nutrient decrypts the document in memory, extraction, conversion, and export all work as they would on an unprotected file.

Nutrient Java SDK supplies the password through the password setting on the document’s open settings. Set it before opening the file, and Nutrient uses it to unlock the document.

Download sample

How Nutrient supports this workflow

The password lives on the open settings, and Nutrient applies it while opening the file — before Document.open returns — so the password has to be in place before the open call. Create a DocumentSettings instance, set the password on its open settings, and pass the instance to Document.open.

Nutrient resolves open settings from the document first and falls back to the SDK-wide settings — a default password set once on the SDK applies to every document, and a password set on a document’s settings overrides it. An unencrypted document ignores the password entirely, so passing one is always safe.

Complete implementation

This example opens a password-protected PDF, exports its content to Markdown, and handles a missing or incorrect password.

Specify a package name and create a new class:

package io.nutrient.Sample;

Import the required classes from the SDK:

import io.nutrient.sdk.Document;
import io.nutrient.sdk.settings.DocumentSettings;
import io.nutrient.sdk.exceptions.InvalidPasswordException;
import io.nutrient.sdk.exceptions.NutrientException;
public class OpenPasswordProtectedPdf {

Create the main method, call the export method, and catch InvalidPasswordException before the general NutrientException to handle password failures separately:

public static void main(String[] args) {
try {
openAndExport();
} catch (InvalidPasswordException e) {
System.err.println("The password is missing or incorrect: " + e.getMessage());
System.exit(1);
} catch (NutrientException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
}
}

Create a dedicated method for the document work that declares throws NutrientException, so main can handle errors at the call site. Create a DocumentSettings instance and set the password on its open settings — Nutrient uses this password to decrypt the file as it opens. Open the encrypted PDF with the settings, using a try-with-resources(opens in a new tab) statement so the document closes automatically, and export the decrypted content to output.md:

private static void openAndExport() throws NutrientException {
DocumentSettings settings = new DocumentSettings();
settings.getOpenSettings().setPassword("test123");
try (Document document = Document.open("input_password_protected.pdf", settings)) {
document.exportAsMarkdown("output.md");
System.out.println("Successfully opened the document and wrote output.md");
}
}
}

The password unlocks the document in memory. Once it’s open, the document behaves like any other — the export reads its content as if the file were never encrypted.

Handle errors

A missing or incorrect password leaves the document encrypted, so Document.open throws an InvalidPasswordException — the example above catches it before the general NutrientException to report the problem or to retry with a different password. An unencrypted document ignores the password, so supplying one never triggers this exception.

Summary

Opening a password-protected PDF takes four steps:

  1. Create a DocumentSettings instance.
  2. Set the password on its open settings.
  3. Open the document with the settings, catching InvalidPasswordException for password failures.
  4. Export the decrypted content to a file.