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

Tile a Watermark Across a PDF Page

See more PDF Watermark Examples

Demonstrates a tiled watermark, which repeats the stamp over the whole page instead of placing it once. Tiling overrides position.align; tile.spacingX and spacingY set the gaps, and tile.stagger offsets alternate rows by half a tile so the pattern does not line up in columns. A faint tiled stamp carrying the name or address is the usual way to mark a document so that a leaked copy can be traced back, and zOrder back keeps it behind the text where it does not hinder reading.

Chilkat PureBasic Downloads

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

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/long_report.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")

    ;  {date:FORMAT} is replaced per page when the watermark is added.  The other placeholders are
    ;  {page}, {pages} and {filename}; {{ gives a literal brace.
    CkJsonObject::ckUpdateString(json,"text","jdoe@example.com  {date:yyyy-MM-dd}")
    CkJsonObject::ckUpdateString(json,"id","trace")
    CkJsonObject::ckUpdateString(json,"zOrder","back")
    CkJsonObject::ckUpdateNumber(json,"font.size","14")
    CkJsonObject::ckUpdateNumber(json,"opacity","0.15")
    CkJsonObject::ckUpdateNumber(json,"rotation","30")

    CkJsonObject::ckUpdateBool(json,"tile.enabled",1)
    CkJsonObject::ckUpdateNumber(json,"tile.spacingX","60")
    CkJsonObject::ckUpdateNumber(json,"tile.spacingY","80")
    CkJsonObject::ckUpdateBool(json,"tile.stagger",1)

    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 "id: " + CkJsonObject::ckStringOf(jsonOut,"id") + " pages: " + CkJsonObject::ckStringOf(jsonOut,"pages")

    success = CkPdf::ckWriteFile(pdf,"qa_output/report_traced.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    ;  Sample output:

    ;  id: trace pages: 1-3


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure