Encrypt PDF using Go

Encrypt PDF File using using User or Owner Password

To easily and securely encrypt documents, you can use Aspose.PDF for Go via C++.

  • The userPassword, if set, is what you need to provide in order to open a PDF. Acrobat/Reader will prompt a user to enter the user password. If it’s not correct, the document will not open.
  • The ownerPassword, if set, controls permissions, such as printing, editing, extracting, commenting, etc. Acrobat/Reader will disallow these things based on the permission settings. Acrobat will require this password if you want to set/change permissions.

The PDF is protected with user and owner passwords, configured with specific access permissions, and encrypted using the AES-128 algorithm with PDF 2.0–compatible security. The encrypted document is then saved to disk.

  1. Create a new PDF document.
  2. Encrypt the PDF document with encrypt method.
  3. Specify a user password to restrict opening the document.
  4. Specify an owner password to control permissions.
  5. Define allowed actions using a permissions bitflag.
  6. Choose AES-128 as the encryption algorithm.
  7. Enable PDF 2.0 encryption for modern security compliance.
  8. Save the secured document using SaveAs, writing it to a new file.

    package main

    import "github.com/aspose-pdf/aspose-pdf-go-cpp"
    import "log"

    func main() {
        // New creates a new PDF-document
        pdf, err := asposepdf.New()
        if err != nil {
            log.Fatal(err)
        }
        // Close() releases allocated resources for PDF-document
        defer pdf.Close()
        // Encrypt(userPassword, ownerPassword, permissions, cryptoAlgorithm, usePdf20) encrypts PDF-document
        err = pdf.Encrypt(
            "userpass",
            "ownerpass",
            asposepdf.PrintDocument|asposepdf.ModifyContent|asposepdf.FillForm,
            asposepdf.AESx128,
            true,
        )
        if err != nil {
            log.Fatal(err)
        }
        // SaveAs(filename string) saves previously opened PDF-document with new filename
        err = pdf.SaveAs("sample_with_password.pdf")
        if err != nil {
            log.Fatal(err)
        }
    }