SQL Server Requires Chilkat v11.7.0+
SQL Server
PDF Placeholders Without a Value: Error, Blank or Keep
See more PDF Text Replacement Examples
Demonstrates the "unusedPlaceholders" option, which decides what happens to a placeholder found on the page that has no value in "placeholders": "error" (the default) makes it a problem with the reason unusedPlaceholder, so nothing is applied; "blank" replaces it with nothing; "keep" leaves it on the page as it is.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 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_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 every placeholder except {{po}} (the purchase order number, which this customer has none of).
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.qty2', '6'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.desc2', 'Galvanized hinge, 4 inch'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.price2', '$12.50'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.amount2', '$75.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.subtotal', '$995.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.taxRate', '7.35%'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.tax', '$73.13'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.total', '$1,068.13'
DECLARE @jsonOut int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT
-- The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
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].field'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'problems[0].reason'
PRINT 'error (default): ok = ' + @iTmp0 + ', problem: ' + @sTmp0 + ' ' + @sTmp1
-- "keep": {{po}} stays on the page, literally.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'unusedPlaceholders', 'keep'
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 'keep: ok = ' + @iTmp0
-- "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'unusedPlaceholders', 'blank'
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 'blank: applied = ' + @iTmp0
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/invoice_no_po.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:
-- error (default): ok = false, problem: po unusedPlaceholder
-- keep: ok = true
-- blank: applied = true
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO