Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.7.0+

PDF Owner Password vs. User Password

See more PDF Protection Examples

Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loJson
LOCAL loJsonOut
LOCAL loUser
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

loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')

*  Opened with the user password.
loUser = CreateObject('Chilkat.Pdf')
loUser.UserPassword = "secret"
lnSuccess = loUser.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (lnSuccess = 0) THEN
    ? loUser.LastErrorText
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loUser
    CANCEL
ENDIF

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

? "opened with: " + loJsonOut.StringOf("loaded.openedWith")

*  Changing the security needs owner rights, so this is refused.
lnSuccess = loUser.Protection("removeEncryption",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? "removeEncryption was refused: the PDF was not opened with its owner password."
ENDIF

*  Opened with the owner password.
loOwner = CreateObject('Chilkat.Pdf')
loOwner.OwnerPassword = "owner-secret"
lnSuccess = loOwner.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (lnSuccess = 0) THEN
    ? loOwner.LastErrorText
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loUser
    RELEASE loOwner
    CANCEL
ENDIF

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

? "opened with: " + loJsonOut.StringOf("loaded.openedWith")

*  Now it is allowed.
lnSuccess = loOwner.Protection("removeEncryption",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loOwner.LastErrorText
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loUser
    RELEASE loOwner
    CANCEL
ENDIF

? "removeEncryption succeeded; will be saved encrypted: " + STR(loJsonOut.BoolOf("pending.encrypted"))

*  Sample output:

*  opened with: user
*  removeEncryption was refused: the PDF was not opened with its owner password.
*  opened with: owner
*  removeEncryption succeeded; will be saved encrypted: False

RELEASE loJson
RELEASE loJsonOut
RELEASE loUser
RELEASE loOwner