Decrypt PDF using Rust

Decrypt PDF File using Owner Password

You can open, decrypt, and save a password-protected PDF document using Aspose.PDF for Rust via C++. The PDF is opened with the owner password, decrypted to remove all security restrictions, and then saved as a new, unprotected PDF.

  1. Use open_with_password to load a password-protected PDF by supplying the file path and the owner password.
  2. Call the decrypt method to remove password protection and all associated security restrictions from the document.
  3. Save the decrypted PDF using save_as.

  use asposepdf::Document;

  fn main() -> Result<(), Box<dyn std::error::Error>> {
      // Open a password-protected PDF-document
      let pdf = Document::open_with_password("sample_with_password.pdf", "ownerpass")?;

      // Decrypt PDF-document
      pdf.decrypt()?;

      // Save the previously opened PDF-document with new filename
      pdf.save_as("sample_decrypt.pdf")?;

      Ok(())
  }