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

Add an Image (Logo) Watermark to a PDF

See more PDF Watermark Examples

Demonstrates an image watermark placed in a corner of every page. The image is a PNG or JPEG given either by path ("image.path") or, as here, as base64 in the JSON itself ("image.base64"), which suits a server that keeps its logo in a database rather than on disk. PNG transparency is kept. Giving only a width keeps the aspect ratio, and the units member says those numbers are inches rather than points.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut

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_Json.UpdateString("type","image")

//  A small PNG logo, base64 encoded.  Use "image.path" instead to name a PNG or JPEG file:
//     ignore = json.UpdateString("image.path","logo.png");
loo_Json.UpdateString("image.base64","iVBORw0KGgoAAAANSUhEUgAAAGAAAAAgCAYAAADtwH1UAAAApElEQVR42u3a7QmAIBgAYTdpo/ZphhZti6IBylLrlXoO/G3cIeFHSgAAFDKM08rCy8Jzg6Ug8UJ0JF+EDuSXRGgxX8+jufxlTk0nFuCCiF360aidXICMiDP5uQgCXAxQI782ggAHEu7IP4sgQOcBIMA35ItgBQhQEqBmtbX6xl//hAUI3gcIELwTFiD4LEiA4NNQAYLvA+BGzJ0wvIrwLggA8DgbwPbrr8c7+lkAAAAASUVORK5CYII=")

//  The size to draw it at.  Give only the width and the height follows the aspect ratio.
loo_Json.UpdateString("units","in")
loo_Json.UpdateNumber("image.width","1.5")

//  Bottom-right, half an inch in from the page edges.
loo_Json.UpdateString("position.align","bottomRight")
loo_Json.UpdateNumber("position.margin","0.5")
loo_Json.UpdateNumber("opacity","0.85")
loo_Json.UpdateString("id","logo")

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

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 "id: " + loo_JsonOut.StringOf("id") + " pages: " + loo_JsonOut.StringOf("pages")

li_Success = loo_Pdf.WriteFile("qa_output/four_pages_logo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

//  Sample output:

//  id: logo pages: 1-4


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut