PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Pdf2
oleobject loo_SigInfo
long li_Verified
li_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
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
destroy loo_Pdf
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/invoice_signed.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
return
end if
Write-Debug "signatures: " + string(loo_Pdf.NumSignatures)
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")
// An audit changes nothing, so it is allowed on a signed PDF.
li_Success = loo_Pdf.Optimize("audit",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
Write-Debug "audit ok, estimated savings: " + string(loo_JsonOut.IntOf("estimatedSavings.total"))
// Optimizing is refused while PreserveSignatures is true.
li_Success = loo_Pdf.Optimize("optimize",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug "Refused while PreserveSignatures is true."
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.
loo_Pdf.PreserveSignatures = 0
li_Success = loo_Pdf.Optimize("optimize",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
li_Success = loo_Pdf.WriteFile("qa_output/invoice_optimized.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
Write-Debug "optimized, bytesSaved: " + string(loo_JsonOut.IntOf("bytesSaved"))
// The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
loo_Pdf2 = create oleobject
li_rc = loo_Pdf2.ConnectToNewObject("Chilkat.Pdf")
li_Success = loo_Pdf2.LoadFile("qa_output/invoice_optimized.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf2.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Pdf2
return
end if
Write-Debug "signatures after: " + string(loo_Pdf2.NumSignatures)
loo_SigInfo = create oleobject
li_rc = loo_SigInfo.ConnectToNewObject("Chilkat.JsonObject")
li_Verified = loo_Pdf2.VerifySignature(0,loo_SigInfo)
Write-Debug "verifies after optimizing: " + string(li_Verified)
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Pdf2
destroy loo_SigInfo