SQL Server Requires Chilkat v11.7.0+
SQL Server
Optimizing a Signed PDF (PreserveSignatures)
See more PDF Optimization Examples
Demonstrates what happens when a signed PDF is optimized. Optimizing rewrites the document, and a rewrite breaks every existing digital signature, so with PreserveSignatures true (the default) Optimize refuses and LastErrorText says why. Setting PreserveSignatures to false allows it, and the signature that was in the file no longer verifies afterwards. "audit" is always allowed, because it changes nothing.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
DECLARE @jsonOut int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT
-- An audit changes nothing, so it is allowed on a signed PDF.
EXEC sp_OAMethod @pdf, 'Optimize', @success OUT, 'audit', @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, 'estimatedSavings.total'
PRINT 'audit ok, estimated savings: ' + @iTmp0
-- Optimizing is refused while PreserveSignatures is true.
EXEC sp_OAMethod @pdf, 'Optimize', @success OUT, 'optimize', @json, @jsonOut
IF @success = 0
BEGIN
PRINT 'Refused while PreserveSignatures is true.'
END
-- Allow the rewrite. Do this only when the signatures no longer matter -- an invoice being
-- archived, say, where the signed original is kept elsewhere.
EXEC sp_OASetProperty @pdf, 'PreserveSignatures', 0
EXEC sp_OAMethod @pdf, 'Optimize', @success OUT, 'optimize', @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 @pdf, 'WriteFile', @success OUT, 'qa_output/invoice_optimized.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_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'bytesSaved'
PRINT 'optimized, bytesSaved: ' + @iTmp0
-- The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
DECLARE @pdf2 int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf2 OUT
EXEC sp_OAMethod @pdf2, 'LoadFile', @success OUT, 'qa_output/invoice_optimized.pdf'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf2, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @pdf2
RETURN
END
EXEC sp_OAGetProperty @pdf2, 'NumSignatures', @iTmp0 OUT
PRINT 'signatures after: ' + @iTmp0
DECLARE @sigInfo int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @sigInfo OUT
DECLARE @verified int
EXEC sp_OAMethod @pdf2, 'VerifySignature', @verified OUT, 0, @sigInfo
PRINT 'verifies after optimizing: ' + @verified
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @pdf2
EXEC @hr = sp_OADestroy @sigInfo
END
GO