PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; The password must be set before loading.
CkPdf::setCkUserPassword(pdf, "secret")
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
; What security does the file have, and may it be modified?
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckProtection(pdf,"getInfo",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "encrypted: " + Str(CkJsonObject::ckBoolOf(jsonOut,"loaded.encrypted"))
Debug "method: " + CkJsonObject::ckStringOf(jsonOut,"loaded.method")
Debug "openedWith: " + CkJsonObject::ckStringOf(jsonOut,"loaded.openedWith")
Debug "may modify: " + Str(CkJsonObject::ckBoolOf(jsonOut,"loaded.permissions.modify"))
success = CkPdf::ckOptimize(pdf,"optimize",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
success = CkPdf::ckWriteFile(pdf,"qa_output/encrypted_optimized.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "bytesSaved: " + Str(CkJsonObject::ckIntOf(jsonOut,"bytesSaved"))
; The output is still encrypted with the same security: loading it without the password fails.
noPassword.i = CkPdf::ckCreate()
If noPassword.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(noPassword,"qa_output/encrypted_optimized.pdf")
q.i = CkJsonObject::ckCreate()
If q.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
qOut.i = CkJsonObject::ckCreate()
If qOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckExtractText(noPassword,"assess",q,qOut)
Debug "passwordRequired without the password: " + Str(CkJsonObject::ckBoolOf(qOut,"quality.passwordRequired"))
; With the password it reads normally.
withPassword.i = CkPdf::ckCreate()
If withPassword.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkPdf::setCkUserPassword(withPassword, "secret")
success = CkPdf::ckLoadFile(withPassword,"qa_output/encrypted_optimized.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(withPassword)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(noPassword)
CkJsonObject::ckDispose(q)
CkJsonObject::ckDispose(qOut)
CkPdf::ckDispose(withPassword)
ProcedureReturn
EndIf
Debug "pages: " + Str(CkPdf::ckNumPages(withPassword))
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(noPassword)
CkJsonObject::ckDispose(q)
CkJsonObject::ckDispose(qOut)
CkPdf::ckDispose(withPassword)
ProcedureReturn
EndProcedure