SQL Server Requires Chilkat v11.7.0+
SQL Server
Replace the Second (or Every) Occurrence of a Label in a PDF
See more PDF Text Replacement Examples
Demonstrates the "occurrence" member of a label entry. By default the first occurrence of a label, in reading order across the selected pages, is replaced; "occurrence" can be a number for the nth, or "all" for every occurrence. The invoice template has "Ref:" twice.Chilkat SQL Server Downloads
--
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 invoice_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/invoice_template.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
-- This template's {{placeholders}} are not being filled here: "keep" leaves them as they are
-- (the default, "error", would refuse to apply while a placeholder on the page has no value).
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'unusedPlaceholders', 'keep'
-- "Ref:" appears twice on the page (A-1 near the top, A-2 below the totals).
-- The default replaces the first occurrence only.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].label', 'Ref:'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].value', 'B-7'
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, 'IntOf', @iTmp0 OUT, 'replacements[0].count'
PRINT 'default: ' + @iTmp0 + ' occurrence replaced'
-- The second occurrence only.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].occurrence', '2'
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, 'IntOf', @iTmp0 OUT, 'replacements[0].count'
PRINT 'occurrence 2: ' + @iTmp0 + ' occurrence replaced'
-- Every occurrence.
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'
PRINT 'occurrence all: ' + @iTmp0 + ' occurrences replaced'
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/invoice_refs.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: 1 occurrence replaced
-- occurrence 2: 1 occurrence replaced
-- occurrence all: 2 occurrences replaced
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO