Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loReader
LOCAL loOwner

lnSuccess = 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

loPdf = CreateObject('Chilkat.Pdf')

*  Load with the current passwords.
loPdf.UserPassword = "secret"
loPdf.OwnerPassword = "owner-secret"
lnSuccess = loPdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

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

loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.Protection("changePasswords",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

*  The algorithm and the permissions are kept.
? "algorithm: " + loJsonOut.StringOf("pending.algorithm")
? "password needed to open: " + STR(loJsonOut.BoolOf("pending.userPasswordRequired"))

lnSuccess = loPdf.WriteFile("qa_output/minutes_new_passwords.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

*  The new user password opens the result.
loReader = CreateObject('Chilkat.Pdf')
loReader.UserPassword = "new-user-secret"
lnSuccess = loReader.LoadFile("qa_output/minutes_new_passwords.pdf")
IF (lnSuccess = 0) THEN
    ? loReader.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    CANCEL
ENDIF

lnSuccess = loReader.Protection("getInfo",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loReader.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    CANCEL
ENDIF

? "with the new user password, opened with: " + loJsonOut.StringOf("loaded.openedWith")

*  And so does the new owner password.
loOwner = CreateObject('Chilkat.Pdf')
loOwner.OwnerPassword = "new-owner-secret"
lnSuccess = loOwner.LoadFile("qa_output/minutes_new_passwords.pdf")
IF (lnSuccess = 0) THEN
    ? loOwner.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    RELEASE loOwner
    CANCEL
ENDIF

lnSuccess = loOwner.Protection("getInfo",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loOwner.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    RELEASE loOwner
    CANCEL
ENDIF

? "with the new owner password, opened with: " + loJsonOut.StringOf("loaded.openedWith")

*  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

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loReader
RELEASE loOwner