Sample code for 30+ languages & platforms
Classic ASP Requires Chilkat v11.7.0+

Audit a PDF First, Optimize Only If It Is Worth It

See more PDF Optimization Examples

Demonstrates deciding whether to rewrite a PDF at all. "audit" costs a fraction of a second and changes nothing, so a batch job can audit every file and optimize only those where enough would be saved. Here the threshold is a tenth of the file; a file already as small as it can be is left alone.

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 watersense_accomplishments.pdf

set pdf = Server.CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

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

fileSize = jsonOut.IntOf("fileSize")
estimated = jsonOut.IntOf("estimatedSavings.total")
Response.Write "<pre>" & Server.HTMLEncode( "fileSize:  " & fileSize) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "estimated: " & estimated) & "</pre>"

'  Only bother when at least a tenth of the file would go.
If (estimated > (fileSize / 10)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Worth optimizing.") & "</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/watersense_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>"
Else
    Response.Write "<pre>" & Server.HTMLEncode( "Already small enough; left as it is.") & "</pre>"
End If


%>
</body>
</html>