How do I lock a PDF document with a password ?

Follow these steps to secure your PDF document with an Owner Password and a User Password

  1. Define the permission that will be granted to user
    import (
      "github.com/unidoc/unipdf/v3/common/license"
      "github.com/unidoc/unipdf/v3/core/security"
      pdf "github.com/unidoc/unipdf/v3/model"
    )
    
    ...
    
    permissions := security.PermPrinting | // Allow printing with low quality
      security.PermFullPrintQuality |
      security.PermModify | // Allow modifications.
      security.PermAnnotate | // Allow annotations.
      security.PermFillForms |
      security.PermRotateInsert | // Allow modifying page order, rotating pages etc.
      security.PermExtractGraphics | // Allow extracting graphics.
      security.PermDisabilityExtract // Allow extracting graphics (accessibility)
    
    encryptOptions := &pdf.EncryptOptions{
      Permissions: permissions,
    }
    	
  2. Supply the Owner Password, User Password, and permissions above into Encrypt function
    pdfWriter := pdf.NewPdfWriter()
    <br>err := pdfWriter.Encrypt([]byte(userPassword), []byte(ownerPassword), encryptOptions)
    	
  3. After you can use the pdfWriter variable to write a the pdf document

For a complete example on how to lock your PDF document, see this code example

https://github.com/unidoc/unipdf-examples/blob/master/security/pdf_protect.go