Sample code for 30+ languages & platforms
Classic ASP Requires Chilkat v11.7.0+

Change the Passwords of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePasswords operation, which gives an encrypted PDF new user and owner passwords and keeps its algorithm and permissions. The PDF is loaded with its current passwords, the two properties are then set to the new ones, and Protection reads them at the time of the call. Both passwords are replaced, so set both: a property left empty becomes an empty password.

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  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

set pdf = Server.CreateObject("Chilkat.Pdf")

'  Load with the current passwords.
pdf.UserPassword = "secret"
pdf.OwnerPassword = "owner-secret"
success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  The new passwords.  Protection reads the properties as they are when it is called.
pdf.UserPassword = "new-user-secret"
pdf.OwnerPassword = "new-owner-secret"

set json = Server.CreateObject("Chilkat.JsonObject")
set jsonOut = Server.CreateObject("Chilkat.JsonObject")
success = pdf.Protection("changePasswords",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  The algorithm and the permissions are kept.
Response.Write "<pre>" & Server.HTMLEncode( "algorithm: " & jsonOut.StringOf("pending.algorithm")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "password needed to open: " & jsonOut.BoolOf("pending.userPasswordRequired")) & "</pre>"

success = pdf.WriteFile("qa_output/minutes_new_passwords.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  The new user password opens the result.
set reader = Server.CreateObject("Chilkat.Pdf")
reader.UserPassword = "new-user-secret"
success = reader.LoadFile("qa_output/minutes_new_passwords.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
    Response.End
End If

success = reader.Protection("getInfo",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "with the new user password, opened with: " & jsonOut.StringOf("loaded.openedWith")) & "</pre>"

'  And so does the new owner password.
set owner = Server.CreateObject("Chilkat.Pdf")
owner.OwnerPassword = "new-owner-secret"
success = owner.LoadFile("qa_output/minutes_new_passwords.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( owner.LastErrorText) & "</pre>"
    Response.End
End If

success = owner.Protection("getInfo",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( owner.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "with the new owner password, opened with: " & jsonOut.StringOf("loaded.openedWith")) & "</pre>"

'  Sample output:

'  algorithm: rc4-128
'  password needed to open: True
'  with the new user password, opened with: user
'  with the new owner password, opened with: owner

%>
</body>
</html>