Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.7.0+

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Pdf2
oleobject loo_Json2

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 two_page_memo.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/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("type","text")
loo_Json.UpdateString("text","UNCONTROLLED WHEN PRINTED")
loo_Json.UpdateString("id","printonly")

//  As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
loo_Json.UpdateString("approach","layer")
loo_Json.UpdateString("layer.name","Print marking")
loo_Json.UpdateString("layer.view","off")
loo_Json.UpdateString("layer.print","on")
loo_Json.UpdateBool("layer.locked",1)

loo_Json.UpdateString("rotation","diagonal")
loo_Json.UpdateString("scale.fit","page")
loo_Json.UpdateNumber("scale.fitPercent","70")
loo_Json.UpdateNumber("opacity","0.3")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

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 "layer id: " + loo_JsonOut.StringOf("id") + " pages: " + loo_JsonOut.StringOf("pages")

li_Success = loo_Pdf.WriteFile("qa_output/memo_print_only.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

//  The same thing as an annotation instead.
loo_Pdf2 = create oleobject
li_rc = loo_Pdf2.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf2.LoadFile("qa_data/pdf/two_page_memo.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

loo_Json2 = create oleobject
li_rc = loo_Json2.ConnectToNewObject("Chilkat.JsonObject")

loo_Json2.UpdateString("type","text")
loo_Json2.UpdateString("text","UNCONTROLLED WHEN PRINTED")
loo_Json2.UpdateString("id","printonly2")
loo_Json2.UpdateString("approach","annotation")
loo_Json2.UpdateBool("annotation.view",0)
loo_Json2.UpdateBool("annotation.print",1)
loo_Json2.UpdateBool("annotation.fixedPrint",1)
loo_Json2.UpdateString("rotation","diagonal")
li_Success = loo_Pdf2.Watermark("add",loo_Json2,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf2
    destroy loo_Json2
    return
end if

Write-Debug "annotation id: " + loo_JsonOut.StringOf("id")

//  Sample output:

//  layer id: printonly pages: 1-2
//  annotation id: printonly2


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Pdf2
destroy loo_Json2