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

Add a CONFIDENTIAL Watermark to Every Page of a PDF

See more PDF Watermark Examples

Demonstrates the simplest watermark: a line of text across every page. "rotation" set to diagonal runs the text from the bottom-left corner to the top-right, computed for each page size, and "scale" with fit page sizes it to fill 80 percent of the page. The watermark is stored as a marked artifact by default, which means viewers show it, text extraction leaves it out, and it can be found and removed again later. Nothing is written until WriteFile is called.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut

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 long_report.pdf

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

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("type", "text")
oJson:UpdateString("text", "CONFIDENTIAL")

//  An id of your own choosing, so that the watermark can be found and removed later.  One is
//  generated when you do not give one.
oJson:UpdateString("id", "confidential")

oJson:UpdateString("color", "#C00000")
oJson:UpdateNumber("opacity", "0.25")
oJson:UpdateString("rotation", "diagonal")
oJson:UpdateString("scale.fit", "page")
oJson:UpdateNumber("scale.fitPercent", "80")

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

//  The id, and the pages it was stamped on.  "pages" defaults to all.
? "id: " + oJsonOut:StringOf("id")
? "pages: " + oJsonOut:StringOf("pages")

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

//  Sample output:

//  id: confidential
//  pages: 1-3

oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()