DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
String sTemp1
Integer iTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/order_confirmation.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
// Both pages have "Status: Pending". With pages "2", only page 2 is searched.
Get ComUpdateString Of hoJson "pages" "2" To iSuccess
Get ComUpdateString Of hoJson "labels[0].label" "Status:" To iSuccess
Get ComUpdateString Of hoJson "labels[0].value" "Shipped" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComTextReplace Of hoPdf "check" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "replacements[0].pages" To sTemp1
Showln "pages 2: found on page(s) " sTemp1
// With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
Get ComUpdateString Of hoJson "pages" "all" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComTextReplace Of hoPdf "check" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "replacements[0].pages" To sTemp1
Showln "pages all, first occurrence: found on page(s) " sTemp1
// Every occurrence on every page.
Get ComUpdateString Of hoJson "labels[0].occurrence" "all" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComTextReplace Of hoPdf "apply" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComIntOf Of hoJsonOut "replacements[0].count" To iTemp1
Get ComStringOf Of hoJsonOut "replacements[0].pages" To sTemp1
Showln "pages all, every occurrence: " iTemp1 " replaced on page(s) " sTemp1
Get ComWriteFile Of hoPdf "qa_output/order_shipped.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// 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
End_Procedure