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

Get the Encryption and Permissions of a Loaded PDF

See more PDF Protection Examples

Demonstrates the getInfo operation of Protection, which reports how the loaded PDF is protected: the method and algorithm, whether a password is needed to open it, how it was opened (with the user password, the owner password, an empty password, or a certificate), and each permission. The loaded member describes the file as it is, the pending member describes the security it will be saved with, and changed says whether the two differ. getInfo is the one operation that does not need owner rights.

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

    --  The password must be set before loading.
    EXEC sp_OASetProperty @pdf, 'UserPassword', '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_OASetProperty @jsonOut, 'EmitCompact', 0
    EXEC sp_OAMethod @jsonOut, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    --  "loaded" is the security the file has.  "pending" is the security it will be saved with, which
    --  is the same until an operation such as encrypt or changePermissions changes it.

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'loaded.algorithm'
    PRINT 'algorithm: ' + @sTmp0

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

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

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'loaded.permissions.print'
    PRINT 'may print: ' + @sTmp0

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

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'changed'
    PRINT 'changed: ' + @iTmp0

    --  Sample output:

    --  {
    --    "changed": false,
    --    "loaded": {
    --      "encrypted": true,
    --      "method": "password",
    --      "algorithm": "rc4-128",
    --      "pdfVersion": "1.3",
    --      "scope": "all",
    --      "userPasswordRequired": true,
    --      "ownerPasswordSet": true,
    --      "openedWith": "user",
    --      "permissions": {
    --        "print": "full",
    --        "modify": true,
    --        "copy": true,
    --        "accessibility": true,
    --        "annotate": true,
    --        "fillForms": true,
    --        "assemble": true
    --      }
    --    },
    --    "pending": {
    --      "encrypted": true,
    --      "method": "password",
    --      "algorithm": "rc4-128",
    --      "pdfVersion": "1.3",
    --      "scope": "all",
    --      "userPasswordRequired": true,
    --      "ownerPasswordSet": true,
    --      "permissions": {
    --        "print": "full",
    --        "modify": true,
    --        "copy": true,
    --        "accessibility": true,
    --        "annotate": true,
    --        "fillForms": true,
    --        "assemble": true
    --      }
    --    }
    --  }
    --  
    --  algorithm: rc4-128
    --  opened with: user
    --  password needed to open: True
    --  may print: full
    --  may copy: True
    --  changed: False

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut


END
GO