Verschlüsselung
PDF Oxide unterstützt die Verschlüsselung von PDFs mit Passwörtern und Berechtigungen unter Verwendung branchenüblicher Algorithmen. Sie können ein Benutzerpasswort (zum Öffnen des Dokuments erforderlich), ein Eigentümerpasswort (für Vollzugriff erforderlich) und detaillierte Berechtigungen zur Steuerung von Drucken, Kopieren und Bearbeiten festlegen.
Schnellstart: Mit Verschlüsselung speichern
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("input.pdf")
doc.set_title("Confidential Report")
# Encrypt with user and owner passwords
doc.save_encrypted("protected.pdf", "user123", "owner456")
WASM
import { WasmPdfDocument } from "pdf-oxide-wasm";
const doc = new WasmPdfDocument(bytes);
doc.setTitle("Confidential Report");
// Encrypt with user and owner passwords (all permissions enabled)
const output = doc.saveEncryptedToBytes(
"user123", "owner456", true, true, true, true
);
doc.free();
Rust
use pdf_oxide::api::Pdf;
let mut doc = Pdf::open("input.pdf")?;
// Simple encryption with user and owner passwords
doc.save_encrypted("protected.pdf", "user123", "owner456")?;
Verschlüsselung mit benutzerdefinierten Berechtigungen
Python
Die Methode save_encrypted akzeptiert Berechtigungs-Flags als Schlüsselwortargumente.
from pdf_oxide import PdfDocument
doc = PdfDocument("input.pdf")
# View-only: no printing, copying, or modifying
doc.save_encrypted(
"readonly.pdf",
"viewpass",
"adminpass",
allow_print=False,
allow_copy=False,
allow_modify=False,
allow_annotate=False,
)
# Allow only printing
doc.save_encrypted(
"print-only.pdf",
"", # No open password required
"adminpass",
allow_print=True,
allow_copy=False,
allow_modify=False,
allow_annotate=False,
)
Python save_encrypted-Parameter
| Parameter | Typ | Standard | Beschreibung |
|---|---|---|---|
path |
str |
required | Output file path |
user_password |
str |
required | Password to open (empty = no open password) |
owner_password |
str |
None |
Full-access password (defaults to user password) |
allow_print |
bool |
True |
Drucken erlauben |
allow_copy |
bool |
True |
Kopieren erlauben text/graphics |
allow_modify |
bool |
True |
Allow modifying the document |
allow_annotate |
bool |
True |
Allow adding annotations |
WASM
import { WasmPdfDocument } from "pdf-oxide-wasm";
const doc = new WasmPdfDocument(bytes);
// View-only: no printing, copying, or modifying
const readonly = doc.saveEncryptedToBytes(
"viewpass", "adminpass", false, false, false, false
);
// Allow only printing (empty user password = no open password)
const printOnly = doc.saveEncryptedToBytes(
"", "adminpass", true, false, false, false
);
doc.free();
Rust
Für vollständige Kontrolle über die Verschlüsselungseinstellungen verwenden Sie VerschlüsselungConfig und SaveOptions.
use pdf_oxide::api::Pdf;
use pdf_oxide::editor::{
VerschlüsselungConfig, VerschlüsselungAlgorithm, Permissions, SaveOptions,
};
let mut doc = Pdf::open("input.pdf")?;
// Build permissions
let mut perms = Permissions::read_only();
perms.print = true; // Drucken erlauben only
// Build encryption config
let config = VerschlüsselungConfig::new("user123", "owner456")
.with_algorithm(VerschlüsselungAlgorithm::Aes256)
.with_permissions(perms);
// Save with encryption
doc.save_with_encryption("protected.pdf", config)?;
VerschlüsselungConfig
Das VerschlüsselungConfig-Struct steuert alle Verschlüsselungsparameter.
use pdf_oxide::editor::{VerschlüsselungConfig, VerschlüsselungAlgorithm, Permissions};
let config = VerschlüsselungConfig {
user_password: "user123".to_string(),
owner_password: "owner456".to_string(),
algorithm: VerschlüsselungAlgorithm::Aes256,
permissions: Permissions::all(),
};
Oder verwenden Sie das Builder-Pattern:
let config = VerschlüsselungConfig::new("user123", "owner456")
.with_algorithm(VerschlüsselungAlgorithm::Aes128)
.with_permissions(Permissions::read_only());
VerschlüsselungConfig-Felder
| Feld | Typ | Beschreibung |
|---|---|---|
user_password |
String |
Password required to open the document |
owner_password |
String |
Password for full access and changing security |
algorithm |
VerschlüsselungAlgorithm |
Verschlüsselung algorithm to use |
permissions |
Permissions |
Access control flags |
Verschlüsselungsalgorithmen
| Algorithm | Description |
|---|---|
VerschlüsselungAlgorithm::Aes256 |
AES-256 (strongest, recommended) |
VerschlüsselungAlgorithm::Aes128 |
AES-128 |
VerschlüsselungAlgorithm::Rc4_128 |
RC4 128-bit (legacy compatibility) |
VerschlüsselungAlgorithm::Rc4_40 |
RC4 40-bit (legacy, weak) |
AES-256 ist der Standard bei Verwendung von save_encrypted() in Python oder der Pdf-API.
Permissions
Das Permissions-Struct steuert, welche Operationen erlaubt sind, wenn ein Dokument mit dem Benutzerpasswort geöffnet wird.
use pdf_oxide::editor::Permissions;
// Allow everything
let all = Permissions::all();
// Restrict everything
let readonly = Permissions::read_only();
Berechtigungsfelder
| Field | Type | Default (all) | Default (read_only) | Description |
|---|---|---|---|---|
print |
bool |
true |
false |
Drucken erlauben |
print_high_quality |
bool |
true |
false |
Allow high-quality printing |
modify |
bool |
true |
false |
Allow modifying content |
copy |
bool |
true |
false |
Kopieren erlauben text/graphics |
annotate |
bool |
true |
false |
Allow adding annotations |
fill_forms |
bool |
true |
false |
Allow filling form fields |
accessibility |
bool |
true |
true |
Allow accessibility extraction |
assemble |
bool |
true |
false |
Allow page assembly operations |
Benutzerdefinierte Berechtigungen
let mut perms = Permissions::read_only();
perms.print = true; // Drucken erlauben
perms.fill_forms = true; // Allow filling forms
perms.accessibility = true; // Always allow for compliance
SaveOptions
Verwenden Sie SaveOptions für vollständige Kontrolle darüber, wie das Dokument geschrieben wird.
use pdf_oxide::editor::{SaveOptions, VerschlüsselungConfig};
// Full rewrite (default)
let opts = SaveOptions::full_rewrite();
// Incremental update (faster, preserves structure)
let opts = SaveOptions::incremental();
// With encryption
let config = VerschlüsselungConfig::new("user", "owner");
let opts = SaveOptions::with_encryption(config);
Verschlüsselte PDFs öffnen
Python
Übergeben Sie das Passwort beim Öffnen des Dokuments.
from pdf_oxide import PdfDocument
doc = PdfDocument("protected.pdf", password="user123")
text = doc.extract_text(0)
print(text)
Rust
use pdf_oxide::PdfDocument;
let doc = PdfDocument::open_with_password("protected.pdf", "user123")?;
let text = doc.extract_text(0)?;
println!("{}", text);
Vollständiger Verschlüsselungs-Workflow
Python
from pdf_oxide import PdfDocument
# Open and modify
doc = PdfDocument("report.pdf")
doc.set_title("Confidential Report")
doc.set_author("Finance Team")
# Save with view-only restrictions
doc.save_encrypted(
"report-protected.pdf",
"", # No password to open
"admin2025", # Eigentümerpasswort for full access
allow_print=True,
allow_copy=False,
allow_modify=False,
)
WASM
import { WasmPdfDocument } from "pdf-oxide-wasm";
const doc = new WasmPdfDocument(bytes);
doc.setTitle("Confidential Report");
doc.setAuthor("Finance Team");
// Save with view-only restrictions (no open password, print allowed)
const output = doc.saveEncryptedToBytes(
"", "admin2025", true, false, false, false
);
doc.free();
Rust
use pdf_oxide::api::Pdf;
use pdf_oxide::editor::{
DocumentEditor, EditableDocument,
VerschlüsselungConfig, VerschlüsselungAlgorithm, Permissions, SaveOptions,
};
// Open and modify
let mut doc = Pdf::open("report.pdf")?;
{
let editor = doc.editor().unwrap();
editor.set_title("Confidential Report");
editor.set_author("Finance Team");
}
// Configure encryption
let permissions = Permissions {
print: true,
print_high_quality: true,
modify: false,
copy: false,
annotate: false,
fill_forms: true,
accessibility: true,
assemble: false,
};
let config = VerschlüsselungConfig::new("", "admin2025")
.with_algorithm(VerschlüsselungAlgorithm::Aes256)
.with_permissions(permissions);
doc.save_with_encryption("report-protected.pdf", config)?;
Mit anderen Einstellungen neu verschlüsseln
Rust
use pdf_oxide::editor::{DocumentEditor, EditableDocument, VerschlüsselungConfig, SaveOptions};
// Open with current password
let mut editor = DocumentEditor::open("old-protected.pdf")?;
// Save with new encryption
let config = VerschlüsselungConfig::new("newuser", "newowner");
let options = SaveOptions::with_encryption(config);
editor.save_with_options("re-encrypted.pdf", options)?;
Vollständige API-Referenz
Pdf-Methoden
| Methode | Rückgabe | Beschreibung |
|---|---|---|
save_encrypted(path, user_pw, owner_pw) |
Result<()> |
Save with AES-256 and full permissions |
save_with_encryption(path, config) |
Result<()> |
Save with custom encryption config |
DocumentEditor-/EditableDocument-Methoden
| Methode | Rückgabe | Beschreibung |
|---|---|---|
save(path) |
Result<()> |
Save with full rewrite (no encryption) |
save_with_options(path, options) |
Result<()> |
Save with custom options |
Konfigurationstypen
| Type | Description |
|---|---|
VerschlüsselungConfig |
User/owner passwords, algorithm, permissions |
VerschlüsselungAlgorithm |
Aes256, Aes128, Rc4_128, Rc4_40 |
Permissions |
Fine-grained access control flags |
SaveOptions |
Full rewrite, incremental, or encrypted save |
Verwandte Seiten
- Bearbeitungsüberblick – Öffnen, Metadaten und Speicher-Workflow
- Formularfeld-Bearbeitung – Formularbearbeitung mit Berechtigungen einschränken
- Schwärzung – Inhalte vor der Verschlüsselung schwärzen
- Seitenoperationen – Seiten vor der endgültigen Verschlüsselung vorbereiten