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

Find Out Whether a PDF Needs a Password Before Loading It

See more PDF Protection Examples

Demonstrates PreLoadCheck, which examines a PDF file without loading it and without any password. It reports whether the file is encrypted, by which method and algorithm, whether a password is needed to open it, and what its permissions are. An application uses it to decide whether to ask for a password, or which certificate it needs, before calling LoadFile. PreLoadCheckBd does the same for a PDF held in memory.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loInfo

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 two_page_memo.pdf

*  The PDF used in this example is available at encrypted_user_password.pdf

*  The PDF used in this example is available at no_copy_permission.pdf

loPdf = CreateObject('Chilkat.Pdf')
loInfo = CreateObject('Chilkat.JsonObject')

*  A PDF that is not encrypted.  Nothing but "encrypted" is reported for it.
lnSuccess = loPdf.PreLoadCheck("qa_data/pdf/two_page_memo.pdf",loInfo)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loInfo
    CANCEL
ENDIF

? "two_page_memo.pdf"
? "  encrypted: " + STR(loInfo.BoolOf("encrypted"))

*  A PDF that needs a password to open.
lnSuccess = loPdf.PreLoadCheck("qa_data/pdf/encrypted_user_password.pdf",loInfo)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loInfo
    CANCEL
ENDIF

? "encrypted_user_password.pdf"
? "  encrypted: " + STR(loInfo.BoolOf("encrypted"))
? "  method: " + loInfo.StringOf("method")
? "  algorithm: " + loInfo.StringOf("algorithm")
? "  openPasswordRequired: " + STR(loInfo.BoolOf("openPasswordRequired"))

*  A PDF that opens without a password but is restricted.  The permissions are readable without
*  any password.
lnSuccess = loPdf.PreLoadCheck("qa_data/pdf/no_copy_permission.pdf",loInfo)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loInfo
    CANCEL
ENDIF

? "no_copy_permission.pdf"
? "  encrypted: " + STR(loInfo.BoolOf("encrypted"))
? "  openPasswordRequired: " + STR(loInfo.BoolOf("openPasswordRequired"))
? "  may copy: " + STR(loInfo.BoolOf("permissions.copy"))

*  The typical use: ask for a password only when one is needed, then load.
lnSuccess = loPdf.PreLoadCheck("qa_data/pdf/encrypted_user_password.pdf",loInfo)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loInfo
    CANCEL
ENDIF

IF (loInfo.BoolOf("openPasswordRequired") = 1) THEN
    *  An application would prompt for the password here.
    loPdf.UserPassword = "secret"
ENDIF

lnSuccess = loPdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loInfo
    CANCEL
ENDIF

? "loaded, pages: " + STR(loPdf.NumPages)

*  Sample output:

*  two_page_memo.pdf
*    encrypted: False
*  encrypted_user_password.pdf
*    encrypted: True
*    method: password
*    algorithm: rc4-128
*    openPasswordRequired: True
*  no_copy_permission.pdf
*    encrypted: True
*    openPasswordRequired: False
*    may copy: False
*  loaded, pages: 1

RELEASE loPdf
RELEASE loInfo