Go Requires Chilkat v11.7.0+
Go
Change the Permissions of an Encrypted PDF
See more PDF Protection Examples
Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.Chilkat Go Downloads
success := false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at encrypted_user_password.pdf
pdf := chilkat.NewPdf()
// Changing the permissions needs owner rights. Both passwords are given here, which is what
// an AES-256 PDF requires for this operation.
pdf.SetUserPassword("secret")
pdf.SetOwnerPassword("owner-secret")
success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
if success == false {
fmt.Println(pdf.LastErrorText())
pdf.DisposePdf()
return
}
// No printing and no copying. Everything not mentioned is granted.
json := chilkat.NewJsonObject()
json.UpdateString("permissions.print","none")
json.UpdateBool("permissions.copy",false)
jsonOut := chilkat.NewJsonObject()
success = pdf.Protection("changePermissions",json,jsonOut)
if success == false {
fmt.Println(pdf.LastErrorText())
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
return
}
fmt.Println("print: ", *jsonOut.StringOf("pending.permissions.print"))
fmt.Println("copy: ", *jsonOut.BoolOf("pending.permissions.copy"))
fmt.Println("modify: ", *jsonOut.BoolOf("pending.permissions.modify"))
fmt.Println("password still needed to open: ", *jsonOut.BoolOf("pending.userPasswordRequired"))
success = pdf.WriteFile("qa_output/minutes_no_print.pdf")
if success == false {
fmt.Println(pdf.LastErrorText())
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
return
}
// The same user password opens the result, now with the new permissions.
reader := chilkat.NewPdf()
reader.SetUserPassword("secret")
success = reader.LoadFile("qa_output/minutes_no_print.pdf")
if success == false {
fmt.Println(reader.LastErrorText())
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
reader.DisposePdf()
return
}
empty := chilkat.NewJsonObject()
info := chilkat.NewJsonObject()
success = reader.Protection("getInfo",empty,info)
if success == false {
fmt.Println(reader.LastErrorText())
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
reader.DisposePdf()
empty.DisposeJsonObject()
info.DisposeJsonObject()
return
}
fmt.Println("saved file, print: ", *info.StringOf("loaded.permissions.print"))
fmt.Println("saved file, copy: ", *info.BoolOf("loaded.permissions.copy"))
// Sample output:
// print: none
// copy: False
// modify: True
// password still needed to open: True
// saved file, print: none
// saved file, copy: False
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
reader.DisposePdf()
empty.DisposeJsonObject()
info.DisposeJsonObject()