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

Watermark Only Some of a PDF's Pages

See more PDF Watermark Examples

Demonstrates choosing the pages to stamp. "pages" takes all (the default), first, last, odd, even, or a list of 1-based pages and ranges such as 1,3,5-9,12- where a trailing dash means "to the end". "excludePages" takes the same syntax and is applied afterwards, which is how you say "every page except the cover". The result reports the pages the watermark actually landed on.

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

    Debug "pages in the document: " + Str(CkPdf::ckNumPages(pdf))

    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

    CkJsonObject::ckUpdateString(json,"type","text")
    CkJsonObject::ckUpdateString(json,"text","INTERNAL")
    CkJsonObject::ckUpdateString(json,"id","internal")

    ;  Every page except the first.
    CkJsonObject::ckUpdateString(json,"pages","all")
    CkJsonObject::ckUpdateString(json,"excludePages","1")
    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 "every page but the cover: " + CkJsonObject::ckStringOf(jsonOut,"pages")

    ;  A list of pages and ranges, on a second copy of the document.
    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","APPENDIX")
    CkJsonObject::ckUpdateString(json2,"id","appendix")
    CkJsonObject::ckUpdateString(json2,"pages","2,4-")
    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 "page 2 and 4 onwards: " + CkJsonObject::ckStringOf(jsonOut,"pages")

    success = CkPdf::ckWriteFile(pdf,"qa_output/four_pages_internal.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:

    ;  pages in the document: 4
    ;  every page but the cover: 2-4
    ;  page 2 and 4 onwards: 2,4


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


    ProcedureReturn
EndProcedure