Skip to content
How to lock a PDF document with a password

How to lock a PDF document with a password

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

  • Define the permission that will be granted to user
import (
 "github.com/unidoc/unipdf/v4/common/license"
 "github.com/unidoc/unipdf/v4/core/security"
 pdf "github.com/unidoc/unipdf/v4/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,
}
  • Supply the Owner Password, User Password, and permissions above into Encrypt function
pdfWriter := pdf.NewPdfWriter()

err := pdfWriter.Encrypt([]byte(userPassword), []byte(ownerPassword), encryptOptions)
  • After you can use the pdfWriter variable to write a the pdf document

Visit our user guide on how to lock your PDF document.