SQL Server Requires Chilkat v11.7.0+
SQL Server
Replace Text in a Signed PDF (PreserveSignatures)
See more PDF Text Replacement Examples
Applying a text replacement rewrites the page content, which invalidates any digital signature on the PDF. While the PreserveSignatures property is true (the default), "apply" is refused on a signed PDF; "check" only reads and is always allowed. Setting PreserveSignatures to false allows the replacement; the signature field stays in the written PDF but no longer verifies, so the document should be signed again afterwards if a signature is wanted.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_signed.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_signed.pdf'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
RETURN
END
EXEC sp_OAGetProperty @pdf, 'NumSignatures', @iTmp0 OUT
PRINT 'Signatures: ' + @iTmp0
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
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'
-- "check" reads only, so it works on a signed 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 'check: ok = ' + @iTmp0
-- "apply" is refused while PreserveSignatures is true (the default).
EXEC sp_OAMethod @pdf, 'TextReplace', @success OUT, 'apply', @json, @jsonOut
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
PRINT 'apply refused: ' + @sTmp0
END
-- Allowing the signature to be broken:
EXEC sp_OASetProperty @pdf, 'PreserveSignatures', 0
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
-- The written PDF is a full rewrite with the replacement. The signature field is still in the document,
-- but the bytes it signed are gone, so it no longer verifies.
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/invoice_reissued_broken_signature.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
EXEC sp_OAGetProperty @pdf, 'NumSignatures', @iTmp0 OUT
PRINT 'Signatures after writing: ' + @iTmp0
DECLARE @sigInfo int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @sigInfo OUT
DECLARE @validated int
EXEC sp_OAMethod @pdf, 'VerifySignature', @validated OUT, 0, @sigInfo
PRINT 'Signature 0 verifies: ' + @validated
-- Sample output:
-- Signatures: 1
-- check: ok = true
-- apply refused: ChilkatLog:
-- TextReplace:
-- DllDate: Nov 7 2026
-- ChilkatVersion: 11.7.0
-- UnlockPrefix: CHLKAT
-- UnlockStatus: 2
-- Architecture: Little Endian; 64-bit
-- Language: Visual C++ 11.0 / x64
-- VerboseLogging: 0
-- replaceTextOp:
-- op: apply
-- The PDF is signed: replacing text rewrites the content and invalidates the signatures (set PreserveSignatures = false to allow it).
-- --replaceTextOp
-- Failed.
-- --TextReplace
-- --ChilkatLog
--
-- applied: true
-- Signatures after writing: 1
-- Signature 0 verifies: false
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @sigInfo
END
GO