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

Extract Text from a Password-Protected PDF

See more PDF Text Extraction Examples

Demonstrates extracting text from a PDF that requires a password to open. The PDF loads without the password (its structure can be read), but the "text" operation fails because the content cannot be decrypted; "assess" reports that a password is required. With the UserPassword property set before loading, extraction works normally.

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 (user password: secret) 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

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

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

    --  The PDF loads without its password: the file's structure is readable, so properties such as
    --  NumPages and the security settings are available...
    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
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    --  ...but the content cannot be decrypted, so extracting text fails.
    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
    IF @success = 0
      BEGIN

        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT 'Cannot extract: ' + @sTmp0
      END

    --  "assess" still works: it reports that a password is required and nothing else.
    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'assess', @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, 'quality.passwordRequired'
    PRINT 'passwordRequired: ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.rating'
    PRINT 'rating: ' + @sTmp0

    --  Set the password and load again.
    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
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @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, 'text'
    PRINT @sTmp0

    --  Sample output:

    --  Cannot extract: ChilkatLog:
    --    ExtractText:
    --      ...
    --        The PDF is encrypted and could not be opened (wrong or missing password); its text cannot be read.
    --      ...
    --  passwordRequired: true
    --  rating: none
    --  Board Minutes (Confidential)
    --  
    --  The board met on 3 November 2026. Present: the chair, the treasurer and four members. The treasurer reported that spending for the season came in under budget.
    --  
    --  The meeting closed at 8.45 p.m.

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


END
GO