Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.7.0+

Change the Permissions of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.

Chilkat SQL Server Downloads

SQL Server
--
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

    --  Changing the permissions needs owner rights.  Both passwords are given here, which is what
    --  an AES-256 PDF requires for this operation.
    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

    --  No printing and no copying.  Everything not mentioned is granted.
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'permissions.print', 'none'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'permissions.copy', 0

    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    EXEC sp_OAMethod @pdf, 'Protection', @success OUT, 'changePermissions', @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, 'StringOf', @sTmp0 OUT, 'pending.permissions.print'
    PRINT 'print: ' + @sTmp0

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'pending.permissions.copy'
    PRINT 'copy: ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'pending.permissions.modify'
    PRINT 'modify: ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'pending.userPasswordRequired'
    PRINT 'password still needed to open: ' + @iTmp0

    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/minutes_no_print.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 same user password opens the result, now with the new permissions.
    DECLARE @reader int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @reader OUT

    EXEC sp_OASetProperty @reader, 'UserPassword', 'secret'
    EXEC sp_OAMethod @reader, 'LoadFile', @success OUT, 'qa_output/minutes_no_print.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 @empty int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @empty OUT

    DECLARE @info int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @info OUT

    EXEC sp_OAMethod @reader, 'Protection', @success OUT, 'getInfo', @empty, @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 @empty
        EXEC @hr = sp_OADestroy @info
        RETURN
      END

    EXEC sp_OAMethod @info, 'StringOf', @sTmp0 OUT, 'loaded.permissions.print'
    PRINT 'saved file, print: ' + @sTmp0

    EXEC sp_OAMethod @info, 'BoolOf', @iTmp0 OUT, 'loaded.permissions.copy'
    PRINT 'saved file, copy: ' + @iTmp0

    --  Sample output:

    --  print: none
    --  copy: False
    --  modify: True
    --  password still needed to open: True
    --  saved file, print: none
    --  saved file, copy: False

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut
    EXEC @hr = sp_OADestroy @reader
    EXEC @hr = sp_OADestroy @empty
    EXEC @hr = sp_OADestroy @info


END
GO