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

Page Numbers, Dates and the File Name in a PDF Watermark

See more PDF Watermark Examples

Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.

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

    CkJsonObject::ckUpdateString(json,"type","text")
    CkJsonObject::ckUpdateString(json,"text","Page {page} of {pages}  -  printed {date:yyyy-MM-dd HH:mm}  -  {filename}")
    CkJsonObject::ckUpdateString(json,"id","pagefoot")
    CkJsonObject::ckUpdateString(json,"position.align","bottom")
    CkJsonObject::ckUpdateNumber(json,"position.margin","24")
    CkJsonObject::ckUpdateNumber(json,"font.size","9")
    CkJsonObject::ckUpdateString(json,"color","#505050")

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

    ;  Roman numerals that start at i on page 2, so that a cover page is not numbered.
    pdf2.i = CkPdf::ckCreate()
    If pdf2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf2,"qa_data/pdf/four_pages.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf2)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf2)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckUpdateString(json2,"type","text")
    CkJsonObject::ckUpdateString(json2,"text","- {page} -")
    CkJsonObject::ckUpdateString(json2,"id","roman")
    CkJsonObject::ckUpdateString(json2,"pages","2-")
    CkJsonObject::ckUpdateInt(json2,"pageNumber.start",1)
    CkJsonObject::ckUpdateString(json2,"pageNumber.style","romanLower")
    CkJsonObject::ckUpdateString(json2,"position.align","bottom")
    CkJsonObject::ckUpdateNumber(json2,"font.size","10")
    success = CkPdf::ckWatermark(pdf2,"add",json2,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf2)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf2)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

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

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

    ;  Sample output:

    ;  footer pages: 1-4
    ;  roman pages: 2-4


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


    ProcedureReturn
EndProcedure