VBScript Requires Chilkat v11.7.0+
VBScript
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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
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
set pdf = CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
set json = CreateObject("Chilkat.JsonObject")
success = json.UpdateString("type","text")
success = json.UpdateString("text","UNCONTROLLED WHEN PRINTED")
success = json.UpdateString("id","printonly")
' As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
success = json.UpdateString("approach","layer")
success = json.UpdateString("layer.name","Print marking")
success = json.UpdateString("layer.view","off")
success = json.UpdateString("layer.print","on")
success = json.UpdateBool("layer.locked",1)
success = json.UpdateString("rotation","diagonal")
success = json.UpdateString("scale.fit","page")
success = json.UpdateNumber("scale.fitPercent","70")
success = json.UpdateNumber("opacity","0.3")
set jsonOut = CreateObject("Chilkat.JsonObject")
success = pdf.Watermark("add",json,jsonOut)
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
outFile.WriteLine("layer id: " & jsonOut.StringOf("id") & " pages: " & jsonOut.StringOf("pages"))
success = pdf.WriteFile("qa_output/memo_print_only.pdf")
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
' The same thing as an annotation instead.
set pdf2 = CreateObject("Chilkat.Pdf")
success = pdf2.LoadFile("qa_data/pdf/two_page_memo.pdf")
If (success = 0) Then
outFile.WriteLine(pdf2.LastErrorText)
WScript.Quit
End If
set json2 = CreateObject("Chilkat.JsonObject")
success = json2.UpdateString("type","text")
success = json2.UpdateString("text","UNCONTROLLED WHEN PRINTED")
success = json2.UpdateString("id","printonly2")
success = json2.UpdateString("approach","annotation")
success = json2.UpdateBool("annotation.view",0)
success = json2.UpdateBool("annotation.print",1)
success = json2.UpdateBool("annotation.fixedPrint",1)
success = json2.UpdateString("rotation","diagonal")
success = pdf2.Watermark("add",json2,jsonOut)
If (success = 0) Then
outFile.WriteLine(pdf2.LastErrorText)
WScript.Quit
End If
outFile.WriteLine("annotation id: " & jsonOut.StringOf("id"))
' Sample output:
' layer id: printonly pages: 1-2
' annotation id: printonly2
outFile.Close