SQL Server Requires Chilkat v11.7.0+
SQL Server
Remove the Restrictions from a PDF
See more PDF Protection Examples
Demonstrates the removeRestrictions operation, which grants every permission. A PDF that needs a password to open stays encrypted and keeps that password. A PDF with no user password, like the one here, has nothing left to protect and is saved unencrypted, the same as removeEncryption. The operation needs owner rights, so the PDF is loaded with its owner 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 no_copy_permission.pdf
DECLARE @pdf int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- This PDF opens without a password, but copying text from it is not permitted.
-- Removing the restrictions 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/no_copy_permission.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.permissions.copy'
PRINT 'may copy, before: ' + @iTmp0
EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'loaded.userPasswordRequired'
PRINT 'password needed to open: ' + @iTmp0
EXEC sp_OAMethod @pdf, 'Protection', @success OUT, 'removeRestrictions', @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
-- With no user password and nothing restricted there is nothing left to protect, so the PDF
-- will be saved unencrypted. A PDF that needs a password to open would stay encrypted and
-- keep its password.
EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'pending.encrypted'
PRINT 'will be saved encrypted: ' + @iTmp0
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/leaflet_unrestricted.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
-- Anyone can now extract the text.
DECLARE @reader int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @reader OUT
EXEC sp_OAMethod @reader, 'LoadFile', @success OUT, 'qa_output/leaflet_unrestricted.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
DECLARE @opts int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @opts OUT
DECLARE @extractOut int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @extractOut OUT
DECLARE @sb int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
EXEC sp_OAMethod @reader, 'ExtractTextSb', @success OUT, 'text', @opts, @sb, @extractOut
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 @opts
EXEC @hr = sp_OADestroy @extractOut
EXEC @hr = sp_OADestroy @sb
RETURN
END
EXEC sp_OAGetProperty @sb, 'Length', @iTmp0 OUT
PRINT 'characters extracted: ' + @iTmp0
-- Sample output:
-- may copy, before: False
-- password needed to open: False
-- will be saved encrypted: False
-- characters extracted: 216
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @reader
EXEC @hr = sp_OADestroy @opts
EXEC @hr = sp_OADestroy @extractOut
EXEC @hr = sp_OADestroy @sb
END
GO