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

Put a Letterhead Behind the First Page of a PDF

See more PDF Watermark Examples

Demonstrates the pdfPage type, which places a page of another PDF as the watermark. It is the way to use a letterhead, a cover overlay or a vector logo that was saved as a PDF, and unlike an image it stays sharp at any size because it is drawn, not resampled. "zOrder" set to back puts it behind the existing content, where a letterhead belongs, and "pages" set to first restricts it to page one.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

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

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

    //  The PDF used in this example is available at letterhead.pdf

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/two_page_memo.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" "pdfPage" To iSuccess
    Get ComUpdateString Of hoJson "pdf.path" "qa_data/pdf/letterhead.pdf" To iSuccess

    //  The page of the source PDF to use (1 by default).  "match" instead of a number places source
    //  page n on target page n, which suits a set of per-page backgrounds.
    Get ComUpdateInt Of hoJson "pdf.page" 1 To iSuccess

    //  Behind the memo's own text, on page one, scaled to the page.
    Get ComUpdateString Of hoJson "zOrder" "back" To iSuccess
    Get ComUpdateString Of hoJson "pages" "first" To iSuccess
    Get ComUpdateString Of hoJson "scale.fit" "page" To iSuccess
    Get ComUpdateString Of hoJson "id" "letterhead" 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

    Get ComStringOf Of hoJsonOut "id" To sTemp1
    Get ComStringOf Of hoJsonOut "pages" To sTemp2
    Showln "id: " sTemp1 " pages: " sTemp2

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

    //  Sample output:

    //  id: letterhead pages: 1


End_Procedure