Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oPdf2
LOCAL oJson2

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

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

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("type", "text")
oJson:UpdateString("text", "Page {page} of {pages}  -  printed {date:yyyy-MM-dd HH:mm}  -  {filename}")
oJson:UpdateString("id", "pagefoot")
oJson:UpdateString("position.align", "bottom")
oJson:UpdateNumber("position.margin", "24")
oJson:UpdateNumber("font.size", "9")
oJson:UpdateString("color", "#505050")

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

? "footer pages: " + oJsonOut:StringOf("pages")

//  Roman numerals that start at i on page 2, so that a cover page is not numbered.
oPdf2 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf2:LoadFile("qa_data/pdf/four_pages.pdf")
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf2:destroy()
    RETURN
ENDIF

oJson2 := CreateObject("Chilkat.JsonObject")
oJson2:UpdateString("type", "text")
oJson2:UpdateString("text", "- {page} -")
oJson2:UpdateString("id", "roman")
oJson2:UpdateString("pages", "2-")
oJson2:UpdateInt("pageNumber.start", 1)
oJson2:UpdateString("pageNumber.style", "romanLower")
oJson2:UpdateString("position.align", "bottom")
oJson2:UpdateNumber("font.size", "10")
nSuccess := oPdf2:Watermark("add", oJson2, oJsonOut)
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf2:destroy()
    oJson2:destroy()
    RETURN
ENDIF

? "roman pages: " + oJsonOut:StringOf("pages")

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

//  Sample output:

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

oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oPdf2:destroy()
oJson2:destroy()