Set permissions for a PDF document using Rust

Set access permissions for a PDF document

A new PDF document is created and protected with user and owner passwords, while specific actions—such as printing, modifying content, and filling forms—are explicitly allowed. The document is then saved with the defined permission restrictions applied.

  1. Create a new PDF document.
  2. Call set_permissions to protect the document.
  3. Specify a user password to restrict access.
  4. Specify an owner password to control security settings.
  5. Define allowed operations using a permissions bitflag.
  6. Save the PDF with permissions applied using save_as.

    use asposepdf::{Document, Permissions};

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

        // Set permissions for PDF-document.
        pdf.set_permissions(
            "userpass",  // User password
            "ownerpass", // Owner password
            Permissions::PRINT_DOCUMENT | Permissions::MODIFY_CONTENT | Permissions::FILL_FORM, // Permissions bitflag
        )?;

        // Save the PDF-document with the updated permissions
        pdf.save_as("sample_with_permissions.pdf")?;

        Ok(())
    }