DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
Handle hoNoPassword
Variant vQ
Handle hoQ
Variant vQ
Out Handle hoQOut
Handle hoWithPassword
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
// The password must be set before loading.
Set ComUserPassword Of hoPdf To "secret"
Get ComLoadFile Of hoPdf "qa_data/pdf/encrypted_user_password.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// What security does the file have, and may it be modified?
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComProtection Of hoPdf "getInfo" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComBoolOf Of hoJsonOut "loaded.encrypted" To bTemp1
Showln "encrypted: " bTemp1
Get ComStringOf Of hoJsonOut "loaded.method" To sTemp1
Showln "method: " sTemp1
Get ComStringOf Of hoJsonOut "loaded.openedWith" To sTemp1
Showln "openedWith: " sTemp1
Get ComBoolOf Of hoJsonOut "loaded.permissions.modify" To bTemp1
Showln "may modify: " bTemp1
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComOptimize Of hoPdf "optimize" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComWriteFile Of hoPdf "qa_output/encrypted_optimized.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComIntOf Of hoJsonOut "bytesSaved" To iTemp1
Showln "bytesSaved: " iTemp1
// The output is still encrypted with the same security: loading it without the password fails.
Get Create (RefClass(cComChilkatPdf)) To hoNoPassword
If (Not(IsComObjectCreated(hoNoPassword))) Begin
Send CreateComObject of hoNoPassword
End
Get ComLoadFile Of hoNoPassword "qa_output/encrypted_optimized.pdf" To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoQ
If (Not(IsComObjectCreated(hoQ))) Begin
Send CreateComObject of hoQ
End
Get Create (RefClass(cComChilkatJsonObject)) To hoQOut
If (Not(IsComObjectCreated(hoQOut))) Begin
Send CreateComObject of hoQOut
End
Get pvComObject of q to q
Get pvComObject of hoQOut to vQOut
Get ComExtractText Of hoNoPassword "assess" q vQOut To iSuccess
Get ComBoolOf Of hoQOut "quality.passwordRequired" To bTemp1
Showln "passwordRequired without the password: " bTemp1
// With the password it reads normally.
Get Create (RefClass(cComChilkatPdf)) To hoWithPassword
If (Not(IsComObjectCreated(hoWithPassword))) Begin
Send CreateComObject of hoWithPassword
End
Set ComUserPassword Of hoWithPassword To "secret"
Get ComLoadFile Of hoWithPassword "qa_output/encrypted_optimized.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoWithPassword To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComNumPages Of hoWithPassword To iTemp1
Showln "pages: " iTemp1
End_Procedure