PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
Watermark a Signed PDF Without Breaking the Signature
See more PDF Watermark Examples
Demonstrates the one approach a signed PDF accepts while its signatures are being preserved. Adding a watermark to the page content rewrites the page, which invalidates every existing signature, so with PreserveSignatures true (the default) only the annotation approach is allowed: an annotation is added as an incremental update that leaves the signed bytes untouched. Removing anything is refused for the same reason. Set PreserveSignatures to false to allow the other approaches, and the signatures in the file stop verifying.Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Check
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")
loo_Json.UpdateString("type","text")
loo_Json.UpdateString("text","PAID")
loo_Json.UpdateString("id","paid")
loo_Json.UpdateString("color","#2E7D32")
loo_Json.UpdateString("position.align","topRight")
loo_Json.UpdateNumber("position.margin","36")
// An artifact watermark changes the page content, so it is refused while the signatures matter.
loo_Json.UpdateString("approach","artifact")
li_Success = loo_Pdf.Watermark("add",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug "The artifact approach was refused on a signed PDF, as it should be."
end if
// An annotation is added without touching the signed bytes.
loo_Json.UpdateString("approach","annotation")
li_Success = loo_Pdf.Watermark("add",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 "annotation added, pages: " + loo_JsonOut.StringOf("pages")
li_Success = loo_Pdf.WriteFile("qa_output/invoice_paid.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// The signature still verifies: the original bytes are unchanged and the annotation was appended.
loo_Check = create oleobject
li_rc = loo_Check.ConnectToNewObject("Chilkat.Pdf")
li_Success = loo_Check.LoadFile("qa_output/invoice_paid.pdf")
if li_Success = 0 then
Write-Debug loo_Check.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Check
return
end if
loo_SigInfo = create oleobject
li_rc = loo_SigInfo.ConnectToNewObject("Chilkat.JsonObject")
li_Verified = loo_Check.VerifySignature(0,loo_SigInfo)
Write-Debug "signature still verifies: " + string(li_Verified)
// Sample output:
// signatures: 1
// The artifact approach was refused on a signed PDF, as it should be.
// annotation added, pages: 1
// signature still verifies: True
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Check
destroy loo_SigInfo