PureBasic Requires Chilkat v11.7.0+
PureBasic
Replace PDF Text on Selected Pages Only
See more PDF Text Replacement Examples
Demonstrates the "pages" option, which limits the search for placeholders and labels to the given pages: "all" (the default), "first", "last", "odd", "even", or a list such as "1,3,5-9". The two-page order confirmation carries the same "Status:" label on both pages; the status is changed on page 2 only, then on both pages.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 order_confirmation.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/order_confirmation.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
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Both pages have "Status: Pending". With pages "2", only page 2 is searched.
CkJsonObject::ckUpdateString(json,"pages","2")
CkJsonObject::ckUpdateString(json,"labels[0].label","Status:")
CkJsonObject::ckUpdateString(json,"labels[0].value","Shipped")
success = CkPdf::ckTextReplace(pdf,"check",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "pages 2: found on page(s) " + CkJsonObject::ckStringOf(jsonOut,"replacements[0].pages")
; With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
CkJsonObject::ckUpdateString(json,"pages","all")
success = CkPdf::ckTextReplace(pdf,"check",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "pages all, first occurrence: found on page(s) " + CkJsonObject::ckStringOf(jsonOut,"replacements[0].pages")
; Every occurrence on every page.
CkJsonObject::ckUpdateString(json,"labels[0].occurrence","all")
success = CkPdf::ckTextReplace(pdf,"apply",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "pages all, every occurrence: " + Str(CkJsonObject::ckIntOf(jsonOut,"replacements[0].count")) + " replaced on page(s) " + CkJsonObject::ckStringOf(jsonOut,"replacements[0].pages")
success = CkPdf::ckWriteFile(pdf,"qa_output/order_shipped.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; Sample output:
; pages 2: found on page(s) 2
; pages all, first occurrence: found on page(s) 1
; pages all, every occurrence: 2 replaced on page(s) 1,2
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure