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

Place and Size a PDF Watermark: Alignment, Margins and Fitting

See more PDF Watermark Examples

Demonstrates where the stamp goes and how large it is drawn. position.align picks one of nine spots, position.margin insets it from the page edges, and position.offsetX and offsetY nudge it from there. For the size, scale.factor multiplies the natural size, scale.fit fits the rotated stamp inside the margin box (none, page, width or height) with scale.fitPercent saying how much of that box to fill, and scale.maxWidth and maxHeight cap the result. The units member applies to all of the measurements except font size, which is always in points.

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

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

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

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

    ;  A banner across the top, inset three quarters of an inch, filling 60% of the width.
    CkJsonObject::ckUpdateString(json,"type","text")
    CkJsonObject::ckUpdateString(json,"text","REVIEW COPY")
    CkJsonObject::ckUpdateString(json,"id","banner")
    CkJsonObject::ckUpdateString(json,"units","in")
    CkJsonObject::ckUpdateString(json,"position.align","top")
    CkJsonObject::ckUpdateNumber(json,"position.margin","0.75")
    CkJsonObject::ckUpdateString(json,"scale.fit","width")
    CkJsonObject::ckUpdateNumber(json,"scale.fitPercent","60")
    CkJsonObject::ckUpdateString(json,"color","#1F4E79")
    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 "banner pages: " + CkJsonObject::ckStringOf(jsonOut,"pages")

    ;  A second stamp in the bottom-left corner, nudged a quarter inch to the right and up, with its
    ;  size capped rather than fitted.
    json2.i = CkJsonObject::ckCreate()
    If json2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json2,"type","text")
    CkJsonObject::ckUpdateString(json2,"text","Not for distribution")
    CkJsonObject::ckUpdateString(json2,"id","footnote")
    CkJsonObject::ckUpdateString(json2,"units","in")
    CkJsonObject::ckUpdateString(json2,"position.align","bottomLeft")
    CkJsonObject::ckUpdateNumber(json2,"position.margin","0.5")
    CkJsonObject::ckUpdateNumber(json2,"position.offsetX","0.25")
    CkJsonObject::ckUpdateNumber(json2,"position.offsetY","-0.25")
    CkJsonObject::ckUpdateNumber(json2,"font.size","9")
    CkJsonObject::ckUpdateNumber(json2,"scale.maxWidth","3.0")
    success = CkPdf::ckWatermark(pdf,"add",json2,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    Debug "footnote pages: " + CkJsonObject::ckStringOf(jsonOut,"pages")

    ;  Each call adds one watermark, so a banner plus a note is two calls.
    success = CkPdf::ckWriteFile(pdf,"qa_output/four_pages_placed.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    ;  Sample output:

    ;  banner pages: 1-4
    ;  footnote pages: 1-4


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


    ProcedureReturn
EndProcedure