SQL Server Requires Chilkat v11.7.0+
SQL Server
Remove the Password and Encryption from a PDF
See more PDF Protection Examples
Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.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
-- Removing the encryption needs owner rights: load with the owner password.
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
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, 'getInfo', @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, 'loaded.encrypted'
PRINT 'encrypted, as loaded: ' + @iTmp0
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'loaded.algorithm'
PRINT 'algorithm: ' + @sTmp0
EXEC sp_OAMethod @pdf, 'Protection', @success OUT, 'removeEncryption', @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, 'pending.encrypted'
PRINT 'will be saved encrypted: ' + @iTmp0
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/minutes_unprotected.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 result is an ordinary PDF that opens without a password.
DECLARE @reader int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @reader OUT
DECLARE @info int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @info OUT
EXEC sp_OAMethod @reader, 'PreLoadCheck', @success OUT, 'qa_output/minutes_unprotected.pdf', @info
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
EXEC @hr = sp_OADestroy @info
RETURN
END
EXEC sp_OAMethod @info, 'BoolOf', @iTmp0 OUT, 'encrypted'
PRINT 'encrypted: ' + @iTmp0
EXEC sp_OAMethod @reader, 'LoadFile', @success OUT, 'qa_output/minutes_unprotected.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
EXEC @hr = sp_OADestroy @info
RETURN
END
EXEC sp_OAGetProperty @reader, 'NumPages', @iTmp0 OUT
PRINT 'pages: ' + @iTmp0
-- Sample output:
-- encrypted, as loaded: True
-- algorithm: rc4-128
-- will be saved encrypted: False
-- encrypted: False
-- pages: 1
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @reader
EXEC @hr = sp_OADestroy @info
END
GO