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

Remove PDF Objects Nothing Refers To

See more PDF Optimization Examples

Demonstrates removeUnreferenced. When a page, an image or a whole section is replaced in an editor, the objects it used often stay in the file with nothing pointing at them any more. They are not old revisions -- they are in the current revision, simply unreachable -- so only a reachability walk from the document catalog finds them. In a publication assembled over several editing passes this is frequently the largest single saving.

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

Response.Write "<pre>" & Server.HTMLEncode( "fileSize: " & jsonOut.IntOf("fileSize")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "removeUnreferenced would save: " & jsonOut.IntOf("estimatedSavings.removeUnreferenced")) & "</pre>"

'  Optimize with the defaults and see how many objects were actually dropped.
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( "objects removed: " & jsonOut.IntOf("actions.removeUnreferenced.count")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "bytes saved:     " & jsonOut.IntOf("actions.removeUnreferenced.bytesSaved")) & "</pre>"

'  Nothing a page needs is ever removed: the walk starts at the document catalog and follows every
'  reference, including the ones in annotations, the structure tree, named destinations and the
'  optional-content configuration.
Response.Write "<pre>" & Server.HTMLEncode( "pages after: " & pdf.NumPages) & "</pre>"

%>
</body>
</html>