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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Json2

li_Success = 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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/four_pages.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

//  A banner across the top, inset three quarters of an inch, filling 60% of the width.
loo_Json.UpdateString("type","text")
loo_Json.UpdateString("text","REVIEW COPY")
loo_Json.UpdateString("id","banner")
loo_Json.UpdateString("units","in")
loo_Json.UpdateString("position.align","top")
loo_Json.UpdateNumber("position.margin","0.75")
loo_Json.UpdateString("scale.fit","width")
loo_Json.UpdateNumber("scale.fitPercent","60")
loo_Json.UpdateString("color","#1F4E79")
li_Success = loo_Pdf.Watermark("add",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "banner pages: " + loo_JsonOut.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.
loo_Json2 = create oleobject
li_rc = loo_Json2.ConnectToNewObject("Chilkat.JsonObject")

loo_Json2.UpdateString("type","text")
loo_Json2.UpdateString("text","Not for distribution")
loo_Json2.UpdateString("id","footnote")
loo_Json2.UpdateString("units","in")
loo_Json2.UpdateString("position.align","bottomLeft")
loo_Json2.UpdateNumber("position.margin","0.5")
loo_Json2.UpdateNumber("position.offsetX","0.25")
loo_Json2.UpdateNumber("position.offsetY","-0.25")
loo_Json2.UpdateNumber("font.size","9")
loo_Json2.UpdateNumber("scale.maxWidth","3.0")
li_Success = loo_Pdf.Watermark("add",loo_Json2,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Json2
    return
end if

Write-Debug "footnote pages: " + loo_JsonOut.StringOf("pages")

//  Each call adds one watermark, so a banner plus a note is two calls.
li_Success = loo_Pdf.WriteFile("qa_output/four_pages_placed.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Json2
    return
end if

//  Sample output:

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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Json2