Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oPdf2
LOCAL oJson2

nSuccess := 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

oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("type", "text")
oJson:UpdateString("text", "UNCONTROLLED WHEN PRINTED")
oJson:UpdateString("id", "printonly")

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

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

oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "layer id: " + oJsonOut:StringOf("id") + " pages: " + oJsonOut:StringOf("pages")

nSuccess := oPdf:WriteFile("qa_output/memo_print_only.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

//  The same thing as an annotation instead.
oPdf2 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf2:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf2:destroy()
    RETURN
ENDIF

oJson2 := CreateObject("Chilkat.JsonObject")
oJson2:UpdateString("type", "text")
oJson2:UpdateString("text", "UNCONTROLLED WHEN PRINTED")
oJson2:UpdateString("id", "printonly2")
oJson2:UpdateString("approach", "annotation")
oJson2:UpdateBool("annotation.view", 0)
oJson2:UpdateBool("annotation.print", 1)
oJson2:UpdateBool("annotation.fixedPrint", 1)
oJson2:UpdateString("rotation", "diagonal")
nSuccess := oPdf2:Watermark("add", oJson2, oJsonOut)
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf2:destroy()
    oJson2:destroy()
    RETURN
ENDIF

? "annotation id: " + oJsonOut:StringOf("id")

//  Sample output:

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

oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oPdf2:destroy()
oJson2:destroy()