PureBasic Requires Chilkat v11.7.0+
PureBasic
A PDF Watermark That Only Appears When Printed
See more PDF Watermark Examples
Demonstrates a watermark that is invisible on screen and appears on paper, which is what a "printed copies are uncontrolled" marking needs. Two approaches can do it: an optional-content layer with layer.view off and layer.print on, which also gives the reader an entry in the layers panel of the viewer (layer.locked stops them switching it), and a Watermark annotation with annotation.view false and annotation.print true. annotation.fixedPrint keeps the printed size and position the same whatever paper it goes on.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 two_page_memo.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/two_page_memo.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"type","text")
CkJsonObject::ckUpdateString(json,"text","UNCONTROLLED WHEN PRINTED")
CkJsonObject::ckUpdateString(json,"id","printonly")
; As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
CkJsonObject::ckUpdateString(json,"approach","layer")
CkJsonObject::ckUpdateString(json,"layer.name","Print marking")
CkJsonObject::ckUpdateString(json,"layer.view","off")
CkJsonObject::ckUpdateString(json,"layer.print","on")
CkJsonObject::ckUpdateBool(json,"layer.locked",1)
CkJsonObject::ckUpdateString(json,"rotation","diagonal")
CkJsonObject::ckUpdateString(json,"scale.fit","page")
CkJsonObject::ckUpdateNumber(json,"scale.fitPercent","70")
CkJsonObject::ckUpdateNumber(json,"opacity","0.3")
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
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 "layer id: " + CkJsonObject::ckStringOf(jsonOut,"id") + " pages: " + CkJsonObject::ckStringOf(jsonOut,"pages")
success = CkPdf::ckWriteFile(pdf,"qa_output/memo_print_only.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; The same thing as an annotation instead.
pdf2.i = CkPdf::ckCreate()
If pdf2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf2,"qa_data/pdf/two_page_memo.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf2)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf2)
ProcedureReturn
EndIf
json2.i = CkJsonObject::ckCreate()
If json2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json2,"type","text")
CkJsonObject::ckUpdateString(json2,"text","UNCONTROLLED WHEN PRINTED")
CkJsonObject::ckUpdateString(json2,"id","printonly2")
CkJsonObject::ckUpdateString(json2,"approach","annotation")
CkJsonObject::ckUpdateBool(json2,"annotation.view",0)
CkJsonObject::ckUpdateBool(json2,"annotation.print",1)
CkJsonObject::ckUpdateBool(json2,"annotation.fixedPrint",1)
CkJsonObject::ckUpdateString(json2,"rotation","diagonal")
success = CkPdf::ckWatermark(pdf2,"add",json2,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf2)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf2)
CkJsonObject::ckDispose(json2)
ProcedureReturn
EndIf
Debug "annotation id: " + CkJsonObject::ckStringOf(jsonOut,"id")
; Sample output:
; layer id: printonly pages: 1-2
; annotation id: printonly2
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf2)
CkJsonObject::ckDispose(json2)
ProcedureReturn
EndProcedure