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

PDF Owner Password vs. User Password

See more PDF Protection Examples

Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.

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 @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

    --  Opened with the user password.
    DECLARE @user int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @user OUT

    EXEC sp_OASetProperty @user, 'UserPassword', 'secret'
    EXEC sp_OAMethod @user, 'LoadFile', @success OUT, 'qa_data/pdf/encrypted_user_password.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @user, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @user
        RETURN
      END
    EXEC sp_OAMethod @user, 'Protection', @success OUT, 'getInfo', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @user, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @user
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'loaded.openedWith'
    PRINT 'opened with: ' + @sTmp0

    --  Changing the security needs owner rights, so this is refused.
    EXEC sp_OAMethod @user, 'Protection', @success OUT, 'removeEncryption', @json, @jsonOut
    IF @success = 0
      BEGIN

        PRINT 'removeEncryption was refused: the PDF was not opened with its owner password.'
      END

    --  Opened with the owner password.
    DECLARE @owner int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @owner OUT

    EXEC sp_OASetProperty @owner, 'OwnerPassword', 'owner-secret'
    EXEC sp_OAMethod @owner, 'LoadFile', @success OUT, 'qa_data/pdf/encrypted_user_password.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @owner, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @user
        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 @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @user
        EXEC @hr = sp_OADestroy @owner
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'loaded.openedWith'
    PRINT 'opened with: ' + @sTmp0

    --  Now it is allowed.
    EXEC sp_OAMethod @owner, 'Protection', @success OUT, 'removeEncryption', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @owner, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @user
        EXEC @hr = sp_OADestroy @owner
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'pending.encrypted'
    PRINT 'removeEncryption succeeded; will be saved encrypted: ' + @iTmp0

    --  Sample output:

    --  opened with: user
    --  removeEncryption was refused: the PDF was not opened with its owner password.
    --  opened with: owner
    --  removeEncryption succeeded; will be saved encrypted: False

    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut
    EXEC @hr = sp_OADestroy @user
    EXEC @hr = sp_OADestroy @owner


END
GO