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

Watermark Only Some of a PDF's Pages

See more PDF Watermark Examples

Demonstrates choosing the pages to stamp. "pages" takes all (the default), first, last, odd, even, or a list of 1-based pages and ranges such as 1,3,5-9,12- where a trailing dash means "to the end". "excludePages" takes the same syntax and is applied afterwards, which is how you say "every page except the cover". The result reports the pages the watermark actually landed on.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Pdf2
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

Write-Debug "pages in the document: " + string(loo_Pdf.NumPages)

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

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

loo_Json.UpdateString("type","text")
loo_Json.UpdateString("text","INTERNAL")
loo_Json.UpdateString("id","internal")

//  Every page except the first.
loo_Json.UpdateString("pages","all")
loo_Json.UpdateString("excludePages","1")
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 "every page but the cover: " + loo_JsonOut.StringOf("pages")

//  A list of pages and ranges, on a second copy of the document.
loo_Pdf2 = create oleobject
li_rc = loo_Pdf2.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf2.LoadFile("qa_data/pdf/four_pages.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf2
    return
end if

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

loo_Json2.UpdateString("type","text")
loo_Json2.UpdateString("text","APPENDIX")
loo_Json2.UpdateString("id","appendix")
loo_Json2.UpdateString("pages","2,4-")
li_Success = loo_Pdf2.Watermark("add",loo_Json2,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf2
    destroy loo_Json2
    return
end if

Write-Debug "page 2 and 4 onwards: " + loo_JsonOut.StringOf("pages")

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

//  Sample output:

//  pages in the document: 4
//  every page but the cover: 2-4
//  page 2 and 4 onwards: 2,4


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Pdf2
destroy loo_Json2