PureBasic Requires Chilkat v11.7.0+
PureBasic
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 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
CkJsonObject::ckUpdateString(json,"type","text")
CkJsonObject::ckUpdateString(json,"text","PAID")
CkJsonObject::ckUpdateString(json,"id","paid")
CkJsonObject::ckUpdateString(json,"color","#2E7D32")
CkJsonObject::ckUpdateString(json,"position.align","topRight")
CkJsonObject::ckUpdateNumber(json,"position.margin","36")
; An artifact watermark changes the page content, so it is refused while the signatures matter.
CkJsonObject::ckUpdateString(json,"approach","artifact")
success = CkPdf::ckWatermark(pdf,"add",json,jsonOut)
If success = 0
Debug "The artifact approach was refused on a signed PDF, as it should be."
EndIf
; An annotation is added without touching the signed bytes.
CkJsonObject::ckUpdateString(json,"approach","annotation")
success = CkPdf::ckWatermark(pdf,"add",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "annotation added, pages: " + CkJsonObject::ckStringOf(jsonOut,"pages")
success = CkPdf::ckWriteFile(pdf,"qa_output/invoice_paid.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; The signature still verifies: the original bytes are unchanged and the annotation was appended.
check.i = CkPdf::ckCreate()
If check.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(check,"qa_output/invoice_paid.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(check)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(check)
ProcedureReturn
EndIf
sigInfo.i = CkJsonObject::ckCreate()
If sigInfo.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
verified.i = CkPdf::ckVerifySignature(check,0,sigInfo)
Debug "signature still verifies: " + Str(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
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(check)
CkJsonObject::ckDispose(sigInfo)
ProcedureReturn
EndProcedure