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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

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

set pdf = CreateObject("Chilkat.Pdf")

'  The password must be set before loading.
pdf.UserPassword = "secret"
success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
If (success = 0) Then
    outFile.WriteLine(pdf.LastErrorText)
    WScript.Quit
End If

'  What security does the file have, and may it be modified?
set json = CreateObject("Chilkat.JsonObject")
set jsonOut = CreateObject("Chilkat.JsonObject")
success = pdf.Protection("getInfo",json,jsonOut)
If (success = 0) Then
    outFile.WriteLine(pdf.LastErrorText)
    WScript.Quit
End If

outFile.WriteLine("encrypted: " & jsonOut.BoolOf("loaded.encrypted"))
outFile.WriteLine("method:    " & jsonOut.StringOf("loaded.method"))
outFile.WriteLine("openedWith: " & jsonOut.StringOf("loaded.openedWith"))
outFile.WriteLine("may modify: " & jsonOut.BoolOf("loaded.permissions.modify"))

success = pdf.Optimize("optimize",json,jsonOut)
If (success = 0) Then
    outFile.WriteLine(pdf.LastErrorText)
    WScript.Quit
End If

success = pdf.WriteFile("qa_output/encrypted_optimized.pdf")
If (success = 0) Then
    outFile.WriteLine(pdf.LastErrorText)
    WScript.Quit
End If

outFile.WriteLine("bytesSaved: " & jsonOut.IntOf("bytesSaved"))

'  The output is still encrypted with the same security: loading it without the password fails.
set noPassword = CreateObject("Chilkat.Pdf")
success = noPassword.LoadFile("qa_output/encrypted_optimized.pdf")
set q = CreateObject("Chilkat.JsonObject")
set qOut = CreateObject("Chilkat.JsonObject")
success = noPassword.ExtractText("assess",q,qOut)
outFile.WriteLine("passwordRequired without the password: " & qOut.BoolOf("quality.passwordRequired"))

'  With the password it reads normally.
set withPassword = CreateObject("Chilkat.Pdf")
withPassword.UserPassword = "secret"
success = withPassword.LoadFile("qa_output/encrypted_optimized.pdf")
If (success = 0) Then
    outFile.WriteLine(withPassword.LastErrorText)
    WScript.Quit
End If

outFile.WriteLine("pages: " & withPassword.NumPages)

outFile.Close