Encrypt PDF using Go
Contents
[
Hide
]
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.
- Create a new PDF document.
- Encrypt the PDF document with encrypt method.
- Specify a user password to restrict opening the document.
- Specify an owner password to control permissions.
- Define allowed actions using a permissions bitflag.
- Choose AES-128 as the encryption algorithm.
- Enable PDF 2.0 encryption for modern security compliance.
- 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)
}
}