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

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oJson2

nSuccess := 0

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

oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/four_pages.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")

//  A banner across the top, inset three quarters of an inch, filling 60% of the width.
oJson:UpdateString("type", "text")
oJson:UpdateString("text", "REVIEW COPY")
oJson:UpdateString("id", "banner")
oJson:UpdateString("units", "in")
oJson:UpdateString("position.align", "top")
oJson:UpdateNumber("position.margin", "0.75")
oJson:UpdateString("scale.fit", "width")
oJson:UpdateNumber("scale.fitPercent", "60")
oJson:UpdateString("color", "#1F4E79")
nSuccess := oPdf:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "banner pages: " + oJsonOut:StringOf("pages")

//  A second stamp in the bottom-left corner, nudged a quarter inch to the right and up, with its
//  size capped rather than fitted.
oJson2 := CreateObject("Chilkat.JsonObject")
oJson2:UpdateString("type", "text")
oJson2:UpdateString("text", "Not for distribution")
oJson2:UpdateString("id", "footnote")
oJson2:UpdateString("units", "in")
oJson2:UpdateString("position.align", "bottomLeft")
oJson2:UpdateNumber("position.margin", "0.5")
oJson2:UpdateNumber("position.offsetX", "0.25")
oJson2:UpdateNumber("position.offsetY", "-0.25")
oJson2:UpdateNumber("font.size", "9")
oJson2:UpdateNumber("scale.maxWidth", "3.0")
nSuccess := oPdf:Watermark("add", oJson2, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oJson2:destroy()
    RETURN
ENDIF

? "footnote pages: " + oJsonOut:StringOf("pages")

//  Each call adds one watermark, so a banner plus a note is two calls.
nSuccess := oPdf:WriteFile("qa_output/four_pages_placed.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oJson2:destroy()
    RETURN
ENDIF

//  Sample output:

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

oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oJson2:destroy()