PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/invoice_signed.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
Debug "signatures: " + Str(CkPdf::ckNumSignatures(pdf))
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; An audit changes nothing, so it is allowed on a signed PDF.
success = CkPdf::ckOptimize(pdf,"audit",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "audit ok, estimated savings: " + Str(CkJsonObject::ckIntOf(jsonOut,"estimatedSavings.total"))
; Optimizing is refused while PreserveSignatures is true.
success = CkPdf::ckOptimize(pdf,"optimize",json,jsonOut)
If success = 0
Debug "Refused while PreserveSignatures is true."
EndIf
; Allow the rewrite. Do this only when the signatures no longer matter -- an invoice being
; archived, say, where the signed original is kept elsewhere.
CkPdf::setCkPreserveSignatures(pdf, 0)
success = CkPdf::ckOptimize(pdf,"optimize",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
success = CkPdf::ckWriteFile(pdf,"qa_output/invoice_optimized.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "optimized, bytesSaved: " + Str(CkJsonObject::ckIntOf(jsonOut,"bytesSaved"))
; The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
pdf2.i = CkPdf::ckCreate()
If pdf2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf2,"qa_output/invoice_optimized.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf2)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf2)
ProcedureReturn
EndIf
Debug "signatures after: " + Str(CkPdf::ckNumSignatures(pdf2))
sigInfo.i = CkJsonObject::ckCreate()
If sigInfo.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
verified.i = CkPdf::ckVerifySignature(pdf2,0,sigInfo)
Debug "verifies after optimizing: " + Str(verified)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf2)
CkJsonObject::ckDispose(sigInfo)
ProcedureReturn
EndProcedure