Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.7.0+

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 SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @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 order_confirmation.pdf

    DECLARE @pdf int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/order_confirmation.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    --  Both pages have "Status:   Pending".  With pages "2", only page 2 is searched.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'pages', '2'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].label', 'Status:'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].value', 'Shipped'
    EXEC sp_OAMethod @pdf, 'TextReplace', @success OUT, 'check', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'replacements[0].pages'
    PRINT 'pages 2: found on page(s) ' + @sTmp0

    --  With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'pages', 'all'
    EXEC sp_OAMethod @pdf, 'TextReplace', @success OUT, 'check', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'replacements[0].pages'
    PRINT 'pages all, first occurrence: found on page(s) ' + @sTmp0

    --  Every occurrence on every page.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].occurrence', 'all'
    EXEC sp_OAMethod @pdf, 'TextReplace', @success OUT, 'apply', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'replacements[0].count'

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'replacements[0].pages'
    PRINT 'pages all, every occurrence: ' + @iTmp0 + ' replaced on page(s) ' + @sTmp0

    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/order_shipped.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        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

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut


END
GO