PureBasic Requires Chilkat v11.7.0+
PureBasic
Put a Letterhead Behind the First Page of a PDF
See more PDF Watermark Examples
Demonstrates the pdfPage type, which places a page of another PDF as the watermark. It is the way to use a letterhead, a cover overlay or a vector logo that was saved as a PDF, and unlike an image it stays sharp at any size because it is drawn, not resampled. "zOrder" set to back puts it behind the existing content, where a letterhead belongs, and "pages" set to first restricts it to page one.Chilkat PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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 two_page_memo.pdf
; The PDF used in this example is available at letterhead.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/two_page_memo.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"type","pdfPage")
CkJsonObject::ckUpdateString(json,"pdf.path","qa_data/pdf/letterhead.pdf")
; The page of the source PDF to use (1 by default). "match" instead of a number places source
; page n on target page n, which suits a set of per-page backgrounds.
CkJsonObject::ckUpdateInt(json,"pdf.page",1)
; Behind the memo's own text, on page one, scaled to the page.
CkJsonObject::ckUpdateString(json,"zOrder","back")
CkJsonObject::ckUpdateString(json,"pages","first")
CkJsonObject::ckUpdateString(json,"scale.fit","page")
CkJsonObject::ckUpdateString(json,"id","letterhead")
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckWatermark(pdf,"add",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "id: " + CkJsonObject::ckStringOf(jsonOut,"id") + " pages: " + CkJsonObject::ckStringOf(jsonOut,"pages")
success = CkPdf::ckWriteFile(pdf,"qa_output/memo_letterhead.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; Sample output:
; id: letterhead pages: 1
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure