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

Optimizing a Signed PDF (PreserveSignatures)

See more PDF Optimization Examples

Demonstrates what happens when a signed PDF is optimized. Optimizing rewrites the document, and a rewrite breaks every existing digital signature, so with PreserveSignatures true (the default) Optimize refuses and LastErrorText says why. Setting PreserveSignatures to false allows it, and the signature that was in the file no longer verifies afterwards. "audit" is always allowed, because it changes nothing.

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

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

Response.Write "<pre>" & Server.HTMLEncode( "signatures: " & pdf.NumSignatures) & "</pre>"

set json = Server.CreateObject("Chilkat.JsonObject")
set jsonOut = Server.CreateObject("Chilkat.JsonObject")

'  An audit changes nothing, so it is allowed on a signed PDF.
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( "audit ok, estimated savings: " & jsonOut.IntOf("estimatedSavings.total")) & "</pre>"

'  Optimizing is refused while PreserveSignatures is true.
success = pdf.Optimize("optimize",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Refused while PreserveSignatures is true.") & "</pre>"
End If

'  Allow the rewrite.  Do this only when the signatures no longer matter -- an invoice being
'  archived, say, where the signed original is kept elsewhere.
pdf.PreserveSignatures = 0
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/invoice_optimized.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

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

'  The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
set pdf2 = Server.CreateObject("Chilkat.Pdf")
success = pdf2.LoadFile("qa_output/invoice_optimized.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf2.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "signatures after: " & pdf2.NumSignatures) & "</pre>"

set sigInfo = Server.CreateObject("Chilkat.JsonObject")
verified = pdf2.VerifySignature(0,sigInfo)
Response.Write "<pre>" & Server.HTMLEncode( "verifies after optimizing: " & verified) & "</pre>"

%>
</body>
</html>