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

Place and Size a PDF Watermark: Alignment, Margins and Fitting

See more PDF Watermark Examples

Demonstrates where the stamp goes and how large it is drawn. position.align picks one of nine spots, position.margin insets it from the page edges, and position.offsetX and offsetY nudge it from there. For the size, scale.factor multiplies the natural size, scale.fit fits the rotated stamp inside the margin box (none, page, width or height) with scale.fitPercent saying how much of that box to fill, and scale.maxWidth and maxHeight cap the result. The units member applies to all of the measurements except font size, which is always in points.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Variant vJson2
    Handle hoJson2
    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 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 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

    //  A banner across the top, inset three quarters of an inch, filling 60% of the width.
    Get ComUpdateString Of hoJson "type" "text" To iSuccess
    Get ComUpdateString Of hoJson "text" "REVIEW COPY" To iSuccess
    Get ComUpdateString Of hoJson "id" "banner" To iSuccess
    Get ComUpdateString Of hoJson "units" "in" To iSuccess
    Get ComUpdateString Of hoJson "position.align" "top" To iSuccess
    Get ComUpdateNumber Of hoJson "position.margin" "0.75" To iSuccess
    Get ComUpdateString Of hoJson "scale.fit" "width" To iSuccess
    Get ComUpdateNumber Of hoJson "scale.fitPercent" "60" To iSuccess
    Get ComUpdateString Of hoJson "color" "#1F4E79" 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 "banner pages: " sTemp1

    //  A second stamp in the bottom-left corner, nudged a quarter inch to the right and up, with its
    //  size capped rather than fitted.
    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" "Not for distribution" To iSuccess
    Get ComUpdateString Of hoJson2 "id" "footnote" To iSuccess
    Get ComUpdateString Of hoJson2 "units" "in" To iSuccess
    Get ComUpdateString Of hoJson2 "position.align" "bottomLeft" To iSuccess
    Get ComUpdateNumber Of hoJson2 "position.margin" "0.5" To iSuccess
    Get ComUpdateNumber Of hoJson2 "position.offsetX" "0.25" To iSuccess
    Get ComUpdateNumber Of hoJson2 "position.offsetY" "-0.25" To iSuccess
    Get ComUpdateNumber Of hoJson2 "font.size" "9" To iSuccess
    Get ComUpdateNumber Of hoJson2 "scale.maxWidth" "3.0" To iSuccess
    Get pvComObject of hoJson2 to vJson2
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "add" vJson2 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 "footnote pages: " sTemp1

    //  Each call adds one watermark, so a banner plus a note is two calls.
    Get ComWriteFile Of hoPdf "qa_output/four_pages_placed.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Sample output:

    //  banner pages: 1-4
    //  footnote pages: 1-4


End_Procedure