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

PDF Placeholders with Other Delimiters: [[name]]

See more PDF Text Replacement Examples

Demonstrates the "placeholderSyntax" option. Placeholders are {{name}} by default; a template written with other delimiters, such as [[name]] or <>, says so with placeholderSyntax, written around the word "name".

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr 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 letter_template.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/letter_template.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    --  The letter reads: "Dear [[name]]," ... "expires on [[expiry]]" ... "[[amount]] per year" ... "[[agent]]".
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholderSyntax', '[[name]]'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.date', 'October 18, 2026'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.name', 'Ms. Rivera'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.expiry', 'December 31, 2026'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.amount', '$299.00'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.agent', 'Dana Whitfield, Account Manager'

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

    EXEC sp_OAMethod @pdf, 'TextReplace', @success OUT, 'apply', @json, @jsonOut
    IF @success = 0
      BEGIN

        EXEC sp_OAMethod @jsonOut, 'Emit', @sTmp0 OUT
        PRINT 'Not applied: ' + @sTmp0
        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
      END

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @count_i OUT, 'replacements'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jsonOut, 'I', @i

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'replacements[i].field'

        PRINT '[[' + @sTmp0 + ']] replaced'
        SELECT @i = @i + 1
      END

    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/letter_rivera.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:

    --  [[date]] replaced
    --  [[name]] replaced
    --  [[expiry]] replaced
    --  [[amount]] replaced
    --  [[agent]] replaced

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


END
GO