Xbase++ Requires Chilkat v11.7.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oPdf2
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
? "pages in the document: " + Str(oPdf:NumPages)
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("type", "text")
oJson:UpdateString("text", "INTERNAL")
oJson:UpdateString("id", "internal")
// Every page except the first.
oJson:UpdateString("pages", "all")
oJson:UpdateString("excludePages", "1")
nSuccess := oPdf:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "every page but the cover: " + oJsonOut:StringOf("pages")
// A list of pages and ranges, on a second copy of the document.
oPdf2 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf2:LoadFile("qa_data/pdf/four_pages.pdf")
IF (nSuccess == 0)
? oPdf2:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oPdf2:destroy()
RETURN
ENDIF
oJson2 := CreateObject("Chilkat.JsonObject")
oJson2:UpdateString("type", "text")
oJson2:UpdateString("text", "APPENDIX")
oJson2:UpdateString("id", "appendix")
oJson2:UpdateString("pages", "2,4-")
nSuccess := oPdf2:Watermark("add", oJson2, oJsonOut)
IF (nSuccess == 0)
? oPdf2:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oPdf2:destroy()
oJson2:destroy()
RETURN
ENDIF
? "page 2 and 4 onwards: " + oJsonOut:StringOf("pages")
nSuccess := oPdf:WriteFile("qa_output/four_pages_internal.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oPdf2:destroy()
oJson2:destroy()
RETURN
ENDIF
// Sample output:
// pages in the document: 4
// every page but the cover: 2-4
// page 2 and 4 onwards: 2,4
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oPdf2:destroy()
oJson2:destroy()