DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
Handle hoPdf2
Variant vSigInfo
Handle hoSigInfo
Boolean iVerified
String sTemp1
Integer iTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/invoice_signed.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComNumSignatures Of hoPdf To iTemp1
Showln "signatures: " iTemp1
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
// An audit changes nothing, so it is allowed on a signed PDF.
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComOptimize Of hoPdf "audit" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComIntOf Of hoJsonOut "estimatedSavings.total" To iTemp1
Showln "audit ok, estimated savings: " iTemp1
// Optimizing is refused while PreserveSignatures is true.
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComOptimize Of hoPdf "optimize" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Showln "Refused while PreserveSignatures is true."
End
// Allow the rewrite. Do this only when the signatures no longer matter -- an invoice being
// archived, say, where the signed original is kept elsewhere.
Set ComPreserveSignatures Of hoPdf To False
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComOptimize Of hoPdf "optimize" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComWriteFile Of hoPdf "qa_output/invoice_optimized.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComIntOf Of hoJsonOut "bytesSaved" To iTemp1
Showln "optimized, bytesSaved: " iTemp1
// The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
Get Create (RefClass(cComChilkatPdf)) To hoPdf2
If (Not(IsComObjectCreated(hoPdf2))) Begin
Send CreateComObject of hoPdf2
End
Get ComLoadFile Of hoPdf2 "qa_output/invoice_optimized.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf2 To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComNumSignatures Of hoPdf2 To iTemp1
Showln "signatures after: " iTemp1
Get Create (RefClass(cComChilkatJsonObject)) To hoSigInfo
If (Not(IsComObjectCreated(hoSigInfo))) Begin
Send CreateComObject of hoSigInfo
End
Get pvComObject of hoSigInfo to vSigInfo
Get ComVerifySignature Of hoPdf2 0 vSigInfo To iVerified
Showln "verifies after optimizing: " iVerified
End_Procedure