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

Page Numbers, Dates and the File Name in a PDF Watermark

See more PDF Watermark Examples

Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
pdf2: HCkPdf;
json2: HCkJsonObject;

begin
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 four_pages.pdf

pdf := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/four_pages.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'type','text');
CkJsonObject_UpdateString(json,'text','Page {page} of {pages}  -  printed {date:yyyy-MM-dd HH:mm}  -  {filename}');
CkJsonObject_UpdateString(json,'id','pagefoot');
CkJsonObject_UpdateString(json,'position.align','bottom');
CkJsonObject_UpdateNumber(json,'position.margin','24');
CkJsonObject_UpdateNumber(json,'font.size','9');
CkJsonObject_UpdateString(json,'color','#505050');

jsonOut := CkJsonObject_Create();
success := CkPdf_Watermark(pdf,'add',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
Memo1.Lines.Add('footer pages: ' + CkJsonObject__stringOf(jsonOut,'pages'));

//  Roman numerals that start at i on page 2, so that a cover page is not numbered.
pdf2 := CkPdf_Create();
success := CkPdf_LoadFile(pdf2,'qa_data/pdf/four_pages.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf2));
    Exit;
  end;
json2 := CkJsonObject_Create();
CkJsonObject_UpdateString(json2,'type','text');
CkJsonObject_UpdateString(json2,'text','- {page} -');
CkJsonObject_UpdateString(json2,'id','roman');
CkJsonObject_UpdateString(json2,'pages','2-');
CkJsonObject_UpdateInt(json2,'pageNumber.start',1);
CkJsonObject_UpdateString(json2,'pageNumber.style','romanLower');
CkJsonObject_UpdateString(json2,'position.align','bottom');
CkJsonObject_UpdateNumber(json2,'font.size','10');
success := CkPdf_Watermark(pdf2,'add',json2,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf2));
    Exit;
  end;
Memo1.Lines.Add('roman pages: ' + CkJsonObject__stringOf(jsonOut,'pages'));

success := CkPdf_WriteFile(pdf,'qa_output/four_pages_numbered.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

//  Sample output:

//  footer pages: 1-4
//  roman pages: 2-4

CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(pdf2);
CkJsonObject_Dispose(json2);