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

Shrink a PDF Losslessly

See more PDF Optimization Examples

Demonstrates the "optimize" operation with the default options, which is the lossless preset: nothing is resampled, recoloured or thrown away, so every page looks exactly as it did and all the text is still there. The changes are kept in memory until WriteFile (or WriteBd) is called. Note that the real saving is "fileSize" minus the size of the file that was written, not "bytesSaved": the rewrite also packs objects into object streams and writes a compact cross-reference stream, which belongs to no option.

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

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

'  An empty JSON means the default options: every lossless optimization, object streams generated,
'  old revisions dropped, deflate level 9.
set json = Server.CreateObject("Chilkat.JsonObject")
set jsonOut = Server.CreateObject("Chilkat.JsonObject")
success = pdf.Optimize("optimize",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  Nothing has been written yet.  WriteFile (or WriteBd) produces the optimized PDF.
success = pdf.WriteFile("qa_output/grand_canyon_optimized.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

set fac = Server.CreateObject("Chilkat.FileAccess")
sizeBefore = jsonOut.IntOf("fileSize")
sizeAfter = fac.FileSize("qa_output/grand_canyon_optimized.pdf")

Response.Write "<pre>" & Server.HTMLEncode( "before: " & sizeBefore) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "after:  " & sizeAfter) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "saved:  " & sizeBefore - sizeAfter) & "</pre>"

'  What the options themselves account for.  This is smaller than the real saving above, because the
'  rewrite's own gains (object streams, the cross-reference stream, the old sections left behind)
'  belong to no single option.
Response.Write "<pre>" & Server.HTMLEncode( "bytesSaved (options only): " & jsonOut.IntOf("bytesSaved")) & "</pre>"

%>
</body>
</html>