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

Optimizing an Encrypted PDF

See more PDF Optimization Examples

Demonstrates optimizing a PDF that is encrypted. Set the password before loading, exactly as for any other operation; the optimized PDF keeps the security it had, so the output still needs the same password. Because Chilkat honours the permissions recorded in the document, optimizing requires owner rights or the "modify" permission: a PDF opened with a user password that forbids modification can be audited but not optimized.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loNoPassword
LOCAL q
LOCAL loQOut
LOCAL loWithPassword

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')

*  The password must be set before loading.
loPdf.UserPassword = "secret"
lnSuccess = loPdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

*  What security does the file have, and may it be modified?
loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.Protection("getInfo",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "encrypted: " + STR(loJsonOut.BoolOf("loaded.encrypted"))
? "method:    " + loJsonOut.StringOf("loaded.method")
? "openedWith: " + loJsonOut.StringOf("loaded.openedWith")
? "may modify: " + STR(loJsonOut.BoolOf("loaded.permissions.modify"))

lnSuccess = loPdf.Optimize("optimize",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

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

? "bytesSaved: " + STR(loJsonOut.IntOf("bytesSaved"))

*  The output is still encrypted with the same security: loading it without the password fails.
loNoPassword = CreateObject('Chilkat.Pdf')
lnSuccess = loNoPassword.LoadFile("qa_output/encrypted_optimized.pdf")
q = CreateObject('Chilkat.JsonObject')
loQOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loNoPassword.ExtractText("assess",q,loQOut)
? "passwordRequired without the password: " + STR(loQOut.BoolOf("quality.passwordRequired"))

*  With the password it reads normally.
loWithPassword = CreateObject('Chilkat.Pdf')
loWithPassword.UserPassword = "secret"
lnSuccess = loWithPassword.LoadFile("qa_output/encrypted_optimized.pdf")
IF (lnSuccess = 0) THEN
    ? loWithPassword.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loNoPassword
    RELEASE q
    RELEASE loQOut
    RELEASE loWithPassword
    CANCEL
ENDIF

? "pages: " + STR(loWithPassword.NumPages)

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loNoPassword
RELEASE q
RELEASE loQOut
RELEASE loWithPassword