Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_NoPassword
oleobject q
oleobject loo_QOut
oleobject loo_WithPassword

li_Success = 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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  The password must be set before loading.
loo_Pdf.UserPassword = "secret"
li_Success = loo_Pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

//  What security does the file have, and may it be modified?
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Pdf.Protection("getInfo",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "encrypted: " + string(loo_JsonOut.BoolOf("loaded.encrypted"))
Write-Debug "method:    " + loo_JsonOut.StringOf("loaded.method")
Write-Debug "openedWith: " + loo_JsonOut.StringOf("loaded.openedWith")
Write-Debug "may modify: " + string(loo_JsonOut.BoolOf("loaded.permissions.modify"))

li_Success = loo_Pdf.Optimize("optimize",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

li_Success = loo_Pdf.WriteFile("qa_output/encrypted_optimized.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "bytesSaved: " + string(loo_JsonOut.IntOf("bytesSaved"))

//  The output is still encrypted with the same security: loading it without the password fails.
loo_NoPassword = create oleobject
li_rc = loo_NoPassword.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_NoPassword.LoadFile("qa_output/encrypted_optimized.pdf")
q = create oleobject
li_rc = q.ConnectToNewObject("Chilkat.JsonObject")

loo_QOut = create oleobject
li_rc = loo_QOut.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_NoPassword.ExtractText("assess",q,loo_QOut)
Write-Debug "passwordRequired without the password: " + string(loo_QOut.BoolOf("quality.passwordRequired"))

//  With the password it reads normally.
loo_WithPassword = create oleobject
li_rc = loo_WithPassword.ConnectToNewObject("Chilkat.Pdf")

loo_WithPassword.UserPassword = "secret"
li_Success = loo_WithPassword.LoadFile("qa_output/encrypted_optimized.pdf")
if li_Success = 0 then
    Write-Debug loo_WithPassword.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_NoPassword
    destroy q
    destroy loo_QOut
    destroy loo_WithPassword
    return
end if

Write-Debug "pages: " + string(loo_WithPassword.NumPages)


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_NoPassword
destroy q
destroy loo_QOut
destroy loo_WithPassword