SQL Server Requires Chilkat v11.7.0+
SQL Server
Check a PDF Text Replacement Before Applying It
See more PDF Text Replacement Examples
Demonstrates the "check" operation, which finds every placeholder and label and applies the font rule but changes nothing, and how to read its report: "ok" says whether "apply" would succeed, "replacements" lists what would be replaced, and "problems" lists what prevents it, each with a reason. Here the first check fails because a placeholder on the page was given no value; the value is added and the check passes, and only then is "apply" called.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 nvarchar(4000)
DECLARE @sTmp2 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
-- Values for most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.customer', 'Acme Corp'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.date', '2026-10-18'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.qty1', '2'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.desc1', 'Cordless drill, 18 V'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.price1', '$460.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.amount1', '$920.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.subtotal', '$920.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.taxRate', '7.35%'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.tax', '$67.62'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.total', '$987.62'
-- "check" reports what "apply" would do, without changing the PDF.
DECLARE @jsonOut int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT
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'
PRINT 'ok: ' + @iTmp0
EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @iTmp0 OUT, 'replacements'
PRINT 'would replace: ' + @iTmp0 + ' placeholders'
-- Each problem names the placeholder (field) or label, and a reason:
-- notFound - the placeholder or label is not on the selected pages
-- unusedPlaceholder - a placeholder on the page was given no value
-- missingGlyph, notInEncoding, nonStandardUnembeddedFont, unsupportedFont - the font rule
-- placeholderSpansFonts - the text is drawn with more than one font or size
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @count_i OUT, 'problems'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jsonOut, 'I', @i
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'problems[i].field'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'problems[i].label'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp2 OUT, 'problems[i].reason'
EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'problems[i].page'
PRINT 'problem: ' + @sTmp0 + @sTmp1 + ' - ' + @sTmp2 + ' (page ' + @iTmp0 + ')'
SELECT @i = @i + 1
END
-- Give the missing placeholders their values and check again.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.po', 'PO-88213'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.qty2', '0'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.desc2', '(no second item)'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.price2', ''
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.amount2', ''
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'
PRINT 'ok after adding the values: ' + @iTmp0
-- Now apply.
EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'ok'
IF @iTmp0 = 1
BEGIN
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'
PRINT 'applied: ' + @iTmp0
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/invoice_checked.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
END
-- Sample output:
-- ok: false
-- would replace: 10 placeholders
-- problem: po - unusedPlaceholder (page 1)
-- problem: qty2 - unusedPlaceholder (page 1)
-- problem: desc2 - unusedPlaceholder (page 1)
-- problem: price2 - unusedPlaceholder (page 1)
-- problem: amount2 - unusedPlaceholder (page 1)
-- ok after adding the values: true
-- applied: true
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO