---
title: "Opening password-protected PDFs | Nutrient Java SDK"
canonical_url: "https://www.nutrient.io/guides/java/extraction/open-password-protected-pdf/"
md_url: "https://www.nutrient.io/guides/java/extraction/open-password-protected-pdf.md"
last_updated: "2026-08-10T00:00:00.000Z"
description: "How to open password-protected PDF documents using Nutrient Java SDK."
---

# Opening password-protected PDFs

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](https://www.nutrient.io/downloads/samples/java/open-password-protected-pdf.zip)

## 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:

```java

package io.nutrient.Sample;

```

Import the required classes from the SDK:

```java

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:

```java

    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](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) statement so the document closes automatically, and export the decrypted content to `output.md`:

```java

    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.
---

## Related pages

- [Nutrient Java SDK extraction guides](/guides/java/extraction.md)
- [Applying OCR to a PDF page](/guides/java/extraction/apply-ocr-to-pdf-page.md)
- [Applying OCR to a PDF document](/guides/java/extraction/apply-ocr-to-pdf.md)
- [Classifying documents](/guides/java/extraction/classify-document.md)
- [Generating image descriptions using Claude](/guides/java/extraction/describe-image-with-claude.md)
- [Generating image descriptions using local AI](/guides/java/extraction/describe-image-with-local-ai.md)
- [Generating image descriptions using OpenAI](/guides/java/extraction/describe-image-with-openai.md)
- [Detecting document language](/guides/java/extraction/detect-document-language.md)
- [Extracting data from images using ICR](/guides/java/extraction/extract-data-from-image-icr.md)
- [Extracting data from images using OCR](/guides/java/extraction/extract-data-from-image-ocr.md)
- [Extracting data from images using vision language models](/guides/java/extraction/extract-data-from-image-vlm.md)
- [Extracting data from specific pages](/guides/java/extraction/extract-data-from-specific-pages.md)
- [Extracting form fields from images](/guides/java/extraction/extract-form-fields-from-image.md)
- [Extracting structured data from documents](/guides/java/extraction/extract-structured-data.md)
- [Generating extraction schemas](/guides/java/extraction/generate-extraction-schema.md)
- [Extracting JSON data from a PDF document](/guides/java/extraction/json-data-extraction.md)
- [Labeling form fields with a vision language model](/guides/java/extraction/label-form-fields-with-vlm.md)
- [Extracting text from PDF documents](/guides/java/extraction/pdf-to-text.md)
- [Reading barcodes with vision extraction](/guides/java/extraction/read-barcodes-with-vision.md)
- [Extracting text from multilingual images](/guides/java/extraction/read-text-from-image-multi-language.md)
- [Extracting text from images](/guides/java/extraction/read-text-from-image.md)
- [Searching document text](/guides/java/extraction/search-document-text.md)
- [Speeding up first ICR operation by predownloading models](/guides/java/extraction/speed-up-first-icr-by-downloading-requirements.md)

