How do I open an encrypted locked PDF document
Encrypted PDF documents can be easily decrypted by UniPDF by following this step below:
- Check if the document is encrypted
pdfReader, _ := model.NewPdfReader(f) isEncrypted, _ := pdfReader.IsEncrypted()
- If the document is encrypted, call Decrypt to decrypt it using a password
if isEncrypted { documentPassword := "unlockthedoc" auth, err := pdfReader.Decrypt([]byte(documentPassword)) if !auth { return errors.New("Decryption failed") } }
- If there's no error, then the document is successfully decrypted and you can continue processing the document.
For a complete example on how to unlock an encrypted PDF document, see this following code example
https://github.com/unidoc/unipdf-examples/blob/master/security/pdf_unlock.go