Visual FoxPro Requires Chilkat v11.7.0+
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loPdf2
LOCAL loSigInfo
LOCAL lnVerified
lnSuccess = 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
loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/invoice_signed.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
CANCEL
ENDIF
? "signatures: " + STR(loPdf.NumSignatures)
loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')
* An audit changes nothing, so it is allowed on a signed PDF.
lnSuccess = loPdf.Optimize("audit",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
? "audit ok, estimated savings: " + STR(loJsonOut.IntOf("estimatedSavings.total"))
* Optimizing is refused while PreserveSignatures is true.
lnSuccess = loPdf.Optimize("optimize",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
? "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.
loPdf.PreserveSignatures = 0
lnSuccess = loPdf.Optimize("optimize",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
lnSuccess = loPdf.WriteFile("qa_output/invoice_optimized.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
? "optimized, bytesSaved: " + STR(loJsonOut.IntOf("bytesSaved"))
* The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
loPdf2 = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf2.LoadFile("qa_output/invoice_optimized.pdf")
IF (lnSuccess = 0) THEN
? loPdf2.LastErrorText
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loPdf2
CANCEL
ENDIF
? "signatures after: " + STR(loPdf2.NumSignatures)
loSigInfo = CreateObject('Chilkat.JsonObject')
lnVerified = loPdf2.VerifySignature(0,loSigInfo)
? "verifies after optimizing: " + STR(lnVerified)
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loPdf2
RELEASE loSigInfo