encrypt

Encrypt PDF-document.

pub fn encrypt(
    &self,
    user_password: &str,
    owner_password: &str,
    permissions: Permissions,
    crypto_algorithm: CryptoAlgorithm,
    use_pdf_20: bool,
) -> Result<(), PdfError>

Arguments

  • user_password - the user password
  • owner_password - the owner password
  • permissions - the allowed permissions (bitflags Permissions)
  • crypto_algorithm - the encryption algorithm (CryptoAlgorithm enum)
  • use_pdf_20 - whether to use PDF 2.0 encryption

Returns

  • Ok(()) - if the operation succeeds
  • Err(PdfError) - if the operation fails

Example

use asposepdf::{CryptoAlgorithm, Document, Permissions};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a new PDF-document
    let pdf = Document::new()?;

    // Encrypt PDF-document
    pdf.encrypt(
        "userpass",  // User password
        "ownerpass", // Owner password
        Permissions::PRINT_DOCUMENT | Permissions::MODIFY_CONTENT | Permissions::FILL_FORM, // Permissions bitmask
        CryptoAlgorithm::AESx128, // Encryption algorithm
        true,                     // Use PDF 2.0 encryption
    )?;

    // Save the encrypted PDF-document
    pdf.save_as("sample_with_password.pdf")?;

    Ok(())
}