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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
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 = Server.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
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  What security does the file have, and may it be modified?
set json = Server.CreateObject("Chilkat.JsonObject")
set jsonOut = Server.CreateObject("Chilkat.JsonObject")
success = pdf.Protection("getInfo",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "encrypted: " & jsonOut.BoolOf("loaded.encrypted")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "method:    " & jsonOut.StringOf("loaded.method")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "openedWith: " & jsonOut.StringOf("loaded.openedWith")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "may modify: " & jsonOut.BoolOf("loaded.permissions.modify")) & "</pre>"

success = pdf.Optimize("optimize",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

success = pdf.WriteFile("qa_output/encrypted_optimized.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "bytesSaved: " & jsonOut.IntOf("bytesSaved")) & "</pre>"

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

'  With the password it reads normally.
set withPassword = Server.CreateObject("Chilkat.Pdf")
withPassword.UserPassword = "secret"
success = withPassword.LoadFile("qa_output/encrypted_optimized.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( withPassword.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "pages: " & withPassword.NumPages) & "</pre>"

%>
</body>
</html>