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

Unicode C
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;

    success = FALSE;

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

    pdf = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/two_page_memo.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        return;
    }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"type",L"pdfPage");
    CkJsonObjectW_UpdateString(json,L"pdf.path",L"qa_data/pdf/letterhead.pdf");

    //  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.
    CkJsonObjectW_UpdateInt(json,L"pdf.page",1);

    //  Behind the memo's own text, on page one, scaled to the page.
    CkJsonObjectW_UpdateString(json,L"zOrder",L"back");
    CkJsonObjectW_UpdateString(json,L"pages",L"first");
    CkJsonObjectW_UpdateString(json,L"scale.fit",L"page");
    CkJsonObjectW_UpdateString(json,L"id",L"letterhead");

    jsonOut = CkJsonObjectW_Create();
    success = CkPdfW_Watermark(pdf,L"add",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"id: %s pages: %s\n",CkJsonObjectW_stringOf(jsonOut,L"id"),CkJsonObjectW_stringOf(jsonOut,L"pages"));

    success = CkPdfW_WriteFile(pdf,L"qa_output/memo_letterhead.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    //  Sample output:

    //  id: letterhead pages: 1


    CkPdfW_Dispose(pdf);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);

    }