Sample code for 30+ languages & platforms
SQL Server

PDF Get Encryption and Permissions Information

See more PDF Signatures Examples

Determine if a PDF is encrypted, and the associated user permissions.

Note: Some PDFs are encrypted but not password-protected. In such cases, encryption is used primarily for preventing unauthorized modifications to the document, but it doesn't restrict access. Therefore, you can open and read the document without a password.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    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.

    DECLARE @pdf int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Load a PDF.
    EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'c:/someDir/sample.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    -- Get information about the PDF that was collected in the call to LoadFile.
    DECLARE @ljd int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @ljd OUT

    EXEC sp_OAMethod @pdf, 'GetLastJsonData', NULL, @ljd

    EXEC sp_OASetProperty @ljd, 'EmitCompact', 0

    EXEC sp_OAMethod @ljd, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Sample output:

    -- {
    --   "pdfVersion": "1.6",
    --   "encrypt": {
    --     "filter": "/Standard",
    --     "keyLength": 128,
    --     "V": 4,
    --     "R": 4,
    --     "P": -1340,
    --     "perm": {
    --       "printLowResolution": "allowed",
    --       "printHighResolution": "allowed",
    --       "modifyOther": "not allowed",
    --       "modifyAnnotations": "allowed",
    --       "modifyForms": "not allowed",
    --       "fillInForms": "allowed",
    --       "assembleDoc": "allowed",
    --       "extractAnyPurpose": "not allowed",
    --       "extractAccessibility": "not allowed"
    --     },
    --     "method": "AESV2"
    --   }
    -- }

    -- Use this online tool to generate parsing code from sample JSON: 
    -- Generate Parsing Code from JSON

    DECLARE @pdfVersion nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @pdfVersion OUT, 'pdfVersion'
    DECLARE @Filter nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @Filter OUT, 'encrypt.filter'
    DECLARE @KeyLength int
    EXEC sp_OAMethod @ljd, 'IntOf', @KeyLength OUT, 'encrypt.keyLength'
    DECLARE @V int
    EXEC sp_OAMethod @ljd, 'IntOf', @V OUT, 'encrypt.V'
    DECLARE @R int
    EXEC sp_OAMethod @ljd, 'IntOf', @R OUT, 'encrypt.R'
    DECLARE @P int
    EXEC sp_OAMethod @ljd, 'IntOf', @P OUT, 'encrypt.P'
    DECLARE @PrintLowResolution nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @PrintLowResolution OUT, 'encrypt.perm.printLowResolution'
    DECLARE @PrintHighResolution nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @PrintHighResolution OUT, 'encrypt.perm.printHighResolution'
    DECLARE @ModifyOther nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @ModifyOther OUT, 'encrypt.perm.modifyOther'
    DECLARE @ModifyAnnotations nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @ModifyAnnotations OUT, 'encrypt.perm.modifyAnnotations'
    DECLARE @ModifyForms nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @ModifyForms OUT, 'encrypt.perm.modifyForms'
    DECLARE @FillInForms nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @FillInForms OUT, 'encrypt.perm.fillInForms'
    DECLARE @AssembleDoc nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @AssembleDoc OUT, 'encrypt.perm.assembleDoc'
    DECLARE @ExtractAnyPurpose nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @ExtractAnyPurpose OUT, 'encrypt.perm.extractAnyPurpose'
    DECLARE @ExtractAccessibility nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @ExtractAccessibility OUT, 'encrypt.perm.extractAccessibility'
    DECLARE @Method nvarchar(4000)
    EXEC sp_OAMethod @ljd, 'StringOf', @Method OUT, 'encrypt.method'

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @ljd


END
GO