PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
Page Numbers, Dates and the File Name in a PDF Watermark
See more PDF Watermark Examples
Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.Chilkat PowerBuilder Downloads
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
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.UpdateString("type","text")
loo_Json.UpdateString("text","Page {page} of {pages} - printed {date:yyyy-MM-dd HH:mm} - {filename}")
loo_Json.UpdateString("id","pagefoot")
loo_Json.UpdateString("position.align","bottom")
loo_Json.UpdateNumber("position.margin","24")
loo_Json.UpdateNumber("font.size","9")
loo_Json.UpdateString("color","#505050")
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 "footer pages: " + loo_JsonOut.StringOf("pages")
// Roman numerals that start at i on page 2, so that a cover page is not numbered.
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","- {page} -")
loo_Json2.UpdateString("id","roman")
loo_Json2.UpdateString("pages","2-")
loo_Json2.UpdateInt("pageNumber.start",1)
loo_Json2.UpdateString("pageNumber.style","romanLower")
loo_Json2.UpdateString("position.align","bottom")
loo_Json2.UpdateNumber("font.size","10")
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 "roman pages: " + loo_JsonOut.StringOf("pages")
li_Success = loo_Pdf.WriteFile("qa_output/four_pages_numbered.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:
// footer pages: 1-4
// roman pages: 2-4
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Pdf2
destroy loo_Json2