SQL Server Requires Chilkat v11.7.0+
SQL Server
What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields
See more PDF Text Replacement Examples
TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.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
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.customer', 'Acme Corp'
-- (The other placeholders are left as they are for this example.)
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'unusedPlaceholders', 'keep'
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_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 'In the page content, customer was replaced ' + @iTmp0 + ' times.'
-- Where else the placeholder occurs, untouched:
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @count_i OUT, 'notReplaced'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jsonOut, 'I', @i
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'notReplaced[i].field'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'notReplaced[i].where'
PRINT 'not replaced: {{' + @sTmp0 + '}} in ' + @sTmp1
SELECT @i = @i + 1
END
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/invoice_customer.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:
-- In the page content, customer was replaced 2 times.
-- not replaced: {{customer}} in metadata
-- not replaced: {{customer}} in linkTarget
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO