How-to guides/Security & Encryption

Set print, copy, and edit permissions on a PDF in Rust

Lock down what readers can do with your PDF. Restrict printing, copying text, editing, and form filling using the PDFluent SDK.

rust
use pdfluent::{PdfDocument, EncryptionOptions, Permissions};

fn main() -> pdfluent::Result<()> {
    let mut doc = PdfDocument::open("input.pdf")?;

    let permissions = Permissions::builder()
        .allow_printing(false)
        .allow_copying(false)
        .allow_editing(false)
        .allow_form_filling(true)
        .build();

    let opts = EncryptionOptions::aes256("owner-secret", "user-secret")
        .with_permissions(permissions);

    doc.encrypt(opts)?;
    doc.save("locked.pdf")?;
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent to your project

Add the dependency to Cargo.toml. The encryption feature includes AES-128 and AES-256 support.

rust
# Cargo.toml
[dependencies]
pdfluent = { version = "0.9", features = ["encryption"] }
2

Open the source document

Open the PDF you want to restrict. If it is already encrypted you must supply the owner password.

rust
let mut doc = PdfDocument::open("input.pdf")?;
// Or, if already encrypted:
// let mut doc = PdfDocument::open_with_password("input.pdf", "current-owner-pw")?;
3

Define the permission set

Use the Permissions builder to choose what is allowed. All operations default to denied when you use the builder.

rust
use pdfluent::Permissions;

let permissions = Permissions::builder()
    .allow_printing(false)       // block all printing
    .allow_high_res_printing(false)
    .allow_copying(false)        // block text/image copying
    .allow_editing(false)        // block page editing
    .allow_annotations(false)    // block adding comments
    .allow_form_filling(true)    // allow form data entry
    .build();
4

Apply AES-256 encryption with the permissions

Pair the permission set with an encryption config. The owner password lets you change permissions later. The user password is what readers enter to open the file.

rust
use pdfluent::EncryptionOptions;

let opts = EncryptionOptions::aes256("owner-secret", "user-secret")
    .with_permissions(permissions);

doc.encrypt(opts)?;
5

Save the output file

Write the encrypted PDF to disk. The original file is not modified.

rust
doc.save("locked.pdf")?;
println!("Saved locked.pdf");

Notes and tips

  • AES-256 encryption requires PDF 1.7 or higher. PDFluent upgrades the PDF version automatically if needed.
  • The owner password must differ from the user password, otherwise most PDF readers ignore the permission flags.
  • Permissions without encryption have no effect. PDF readers only enforce flags when the file is encrypted.
  • To remove permissions later, open the file with the owner password and call doc.remove_encryption().

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions