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

A PDF Label That Is Not on the Page: notFound or Ignore

See more PDF Text Replacement Examples

Demonstrates the "unmatchedValues" option. A label or placeholder given a value but not found on the selected pages is a problem with the reason notFound, which stops "apply", because a missing label usually means the wrong document or a typo in the label. With unmatchedValues "ignore" such a value is skipped and the rest is applied -- the right choice when one set of values serves several document layouts.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 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 invoice_labels.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/invoice_labels.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

    --  "Tracking Number:" is not on this invoice.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].label', 'Invoice Number:'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].value', 'INV-2026-0500'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[1].label', 'Tracking Number:'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[1].value', '1Z999AA10123456784'

    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, 'BoolOf', @iTmp0 OUT, 'ok'

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'problems[0].label'

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'problems[0].reason'
    PRINT 'default: ok = ' + @iTmp0 + ', problem: "' + @sTmp0 + '" ' + @sTmp1

    --  With "ignore", the label that is not there is skipped.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'unmatchedValues', 'ignore'
    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, 'BoolOf', @iTmp0 OUT, 'applied'

    EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @iTmp1 OUT, 'replacements'
    PRINT 'ignore: applied = ' + @iTmp0 + ', replacements: ' + @iTmp1

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

    --  default: ok = false, problem: "Tracking Number:" notFound
    --  ignore: applied = true, replacements: 1

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


END
GO