SQL Server Requires Chilkat v11.7.0+
SQL Server
Change the Passwords of an Encrypted PDF
See more PDF Protection Examples
Demonstrates the changePasswords operation, which gives an encrypted PDF new user and owner passwords and keeps its algorithm and permissions. The PDF is loaded with its current passwords, the two properties are then set to the new ones, and Protection reads them at the time of the call. Both passwords are replaced, so set both: a property left empty becomes an empty password.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 encrypted_user_password.pdf
DECLARE @pdf int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Load with the current passwords.
EXEC sp_OASetProperty @pdf, 'UserPassword', 'secret'
EXEC sp_OASetProperty @pdf, 'OwnerPassword', 'owner-secret'
EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/encrypted_user_password.pdf'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
RETURN
END
-- The new passwords. Protection reads the properties as they are when it is called.
EXEC sp_OASetProperty @pdf, 'UserPassword', 'new-user-secret'
EXEC sp_OASetProperty @pdf, 'OwnerPassword', 'new-owner-secret'
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, 'changePasswords', @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
-- The algorithm and the permissions are kept.
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'pending.algorithm'
PRINT 'algorithm: ' + @sTmp0
EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'pending.userPasswordRequired'
PRINT 'password needed to open: ' + @iTmp0
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/minutes_new_passwords.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
-- The new user password opens the result.
DECLARE @reader int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @reader OUT
EXEC sp_OASetProperty @reader, 'UserPassword', 'new-user-secret'
EXEC sp_OAMethod @reader, 'LoadFile', @success OUT, 'qa_output/minutes_new_passwords.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 @reader
RETURN
END
EXEC sp_OAMethod @reader, 'Protection', @success OUT, 'getInfo', @json, @jsonOut
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 @reader
RETURN
END
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'loaded.openedWith'
PRINT 'with the new user password, opened with: ' + @sTmp0
-- And so does the new owner password.
DECLARE @owner int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @owner OUT
EXEC sp_OASetProperty @owner, 'OwnerPassword', 'new-owner-secret'
EXEC sp_OAMethod @owner, 'LoadFile', @success OUT, 'qa_output/minutes_new_passwords.pdf'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @owner, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @reader
EXEC @hr = sp_OADestroy @owner
RETURN
END
EXEC sp_OAMethod @owner, 'Protection', @success OUT, 'getInfo', @json, @jsonOut
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @owner, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @reader
EXEC @hr = sp_OADestroy @owner
RETURN
END
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'loaded.openedWith'
PRINT 'with the new owner password, opened with: ' + @sTmp0
-- Sample output:
-- algorithm: rc4-128
-- password needed to open: True
-- with the new user password, opened with: user
-- with the new owner password, opened with: owner
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @reader
EXEC @hr = sp_OADestroy @owner
END
GO