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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Handle hoPdf2
    Variant vJson2
    Handle hoJson2
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/four_pages.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumPages Of hoPdf To iTemp1
    Showln "pages in the document: " iTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get ComUpdateString Of hoJson "type" "text" To iSuccess
    Get ComUpdateString Of hoJson "text" "INTERNAL" To iSuccess
    Get ComUpdateString Of hoJson "id" "internal" To iSuccess

    //  Every page except the first.
    Get ComUpdateString Of hoJson "pages" "all" To iSuccess
    Get ComUpdateString Of hoJson "excludePages" "1" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "add" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "pages" To sTemp1
    Showln "every page but the cover: " sTemp1

    //  A list of pages and ranges, on a second copy of the document.
    Get Create (RefClass(cComChilkatPdf)) To hoPdf2
    If (Not(IsComObjectCreated(hoPdf2))) Begin
        Send CreateComObject of hoPdf2
    End
    Get ComLoadFile Of hoPdf2 "qa_data/pdf/four_pages.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson2
    If (Not(IsComObjectCreated(hoJson2))) Begin
        Send CreateComObject of hoJson2
    End
    Get ComUpdateString Of hoJson2 "type" "text" To iSuccess
    Get ComUpdateString Of hoJson2 "text" "APPENDIX" To iSuccess
    Get ComUpdateString Of hoJson2 "id" "appendix" To iSuccess
    Get ComUpdateString Of hoJson2 "pages" "2,4-" To iSuccess
    Get pvComObject of hoJson2 to vJson2
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf2 "add" vJson2 vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "pages" To sTemp1
    Showln "page 2 and 4 onwards: " sTemp1

    Get ComWriteFile Of hoPdf "qa_output/four_pages_internal.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Sample output:

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


End_Procedure