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

Add a CONFIDENTIAL Watermark to Every Page of a PDF

See more PDF Watermark Examples

Demonstrates the simplest watermark: a line of text across every page. "rotation" set to diagonal runs the text from the bottom-left corner to the top-right, computed for each page size, and "scale" with fit page sizes it to fill 80 percent of the page. The watermark is stored as a marked artifact by default, which means viewers show it, text extraction leaves it out, and it can be found and removed again later. Nothing is written until WriteFile is called.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    String sTemp1

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

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

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

    //  An id of your own choosing, so that the watermark can be found and removed later.  One is
    //  generated when you do not give one.
    Get ComUpdateString Of hoJson "id" "confidential" To iSuccess

    Get ComUpdateString Of hoJson "color" "#C00000" To iSuccess
    Get ComUpdateNumber Of hoJson "opacity" "0.25" To iSuccess
    Get ComUpdateString Of hoJson "rotation" "diagonal" To iSuccess
    Get ComUpdateString Of hoJson "scale.fit" "page" To iSuccess
    Get ComUpdateNumber Of hoJson "scale.fitPercent" "80" To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    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

    //  The id, and the pages it was stamped on.  "pages" defaults to all.
    Get ComStringOf Of hoJsonOut "id" To sTemp1
    Showln "id: " sTemp1
    Get ComStringOf Of hoJsonOut "pages" To sTemp1
    Showln "pages: " sTemp1

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

    //  Sample output:

    //  id: confidential
    //  pages: 1-3


End_Procedure