SQL Server Requires Chilkat v11.7.0+
SQL Server
Password-Protect a PDF with AES-256 Encryption
See more PDF Protection Examples
Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.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 two_page_memo.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/two_page_memo.pdf'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
RETURN
END
-- The passwords are taken from these two properties at the time Protection is called.
-- The user password opens the PDF. The owner password opens it too, and is the one needed
-- later to change or remove the protection.
EXEC sp_OASetProperty @pdf, 'UserPassword', 'user-secret'
EXEC sp_OASetProperty @pdf, 'OwnerPassword', 'owner-secret'
-- An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
DECLARE @jsonOut int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT
EXEC sp_OAMethod @pdf, 'Protection', @success OUT, 'encrypt', @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
-- Nothing has been encrypted yet. "pending" describes the security the PDF will be saved with.
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'pending.algorithm'
PRINT 'algorithm: ' + @sTmp0
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'pending.pdfVersion'
PRINT 'PDF version: ' + @sTmp0
EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'pending.userPasswordRequired'
PRINT 'password needed to open: ' + @iTmp0
-- Writing the file applies it.
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/memo_encrypted.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
-- PreLoadCheck reports a file's encryption without loading it, and needs no password.
DECLARE @checker int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @checker OUT
DECLARE @info int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @info OUT
EXEC sp_OAMethod @checker, 'PreLoadCheck', @success OUT, 'qa_output/memo_encrypted.pdf', @info
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @checker, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @checker
EXEC @hr = sp_OADestroy @info
RETURN
END
EXEC sp_OAMethod @info, 'BoolOf', @iTmp0 OUT, 'encrypted'
PRINT 'encrypted: ' + @iTmp0
EXEC sp_OAMethod @info, 'BoolOf', @iTmp0 OUT, 'openPasswordRequired'
PRINT 'openPasswordRequired: ' + @iTmp0
-- With the password, the PDF opens normally.
DECLARE @reader int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @reader OUT
EXEC sp_OASetProperty @reader, 'UserPassword', 'user-secret'
EXEC sp_OAMethod @reader, 'LoadFile', @success OUT, 'qa_output/memo_encrypted.pdf'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @reader, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @checker
EXEC @hr = sp_OADestroy @info
EXEC @hr = sp_OADestroy @reader
RETURN
END
EXEC sp_OAGetProperty @reader, 'NumPages', @iTmp0 OUT
PRINT 'pages: ' + @iTmp0
-- Sample output:
-- algorithm: aes-256
-- PDF version: 1.7
-- password needed to open: True
-- encrypted: True
-- openPasswordRequired: True
-- pages: 2
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @checker
EXEC @hr = sp_OADestroy @info
EXEC @hr = sp_OADestroy @reader
END
GO