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

Optimizing an Encrypted PDF

See more PDF Optimization Examples

Demonstrates optimizing a PDF that is encrypted. Set the password before loading, exactly as for any other operation; the optimized PDF keeps the security it had, so the output still needs the same password. Because Chilkat honours the permissions recorded in the document, optimizing requires owner rights or the "modify" permission: a PDF opened with a user password that forbids modification can be audited but not optimized.

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

    --  What security does the file have, and may it be modified?
    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_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'loaded.encrypted'
    PRINT 'encrypted: ' + @iTmp0

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

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

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

    EXEC sp_OAMethod @pdf, 'Optimize', @success OUT, 'optimize', @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 @pdf, 'WriteFile', @success OUT, 'qa_output/encrypted_optimized.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 @jsonOut, 'IntOf', @iTmp0 OUT, 'bytesSaved'
    PRINT 'bytesSaved: ' + @iTmp0

    --  The output is still encrypted with the same security: loading it without the password fails.
    DECLARE @noPassword int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @noPassword OUT

    EXEC sp_OAMethod @noPassword, 'LoadFile', @success OUT, 'qa_output/encrypted_optimized.pdf'
    DECLARE @q int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @q OUT

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

    EXEC sp_OAMethod @noPassword, 'ExtractText', @success OUT, 'assess', @q, @qOut

    EXEC sp_OAMethod @qOut, 'BoolOf', @iTmp0 OUT, 'quality.passwordRequired'
    PRINT 'passwordRequired without the password: ' + @iTmp0

    --  With the password it reads normally.
    DECLARE @withPassword int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @withPassword OUT

    EXEC sp_OASetProperty @withPassword, 'UserPassword', 'secret'
    EXEC sp_OAMethod @withPassword, 'LoadFile', @success OUT, 'qa_output/encrypted_optimized.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @withPassword, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @noPassword
        EXEC @hr = sp_OADestroy @q
        EXEC @hr = sp_OADestroy @qOut
        EXEC @hr = sp_OADestroy @withPassword
        RETURN
      END

    EXEC sp_OAGetProperty @withPassword, 'NumPages', @iTmp0 OUT
    PRINT 'pages: ' + @iTmp0

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut
    EXEC @hr = sp_OADestroy @noPassword
    EXEC @hr = sp_OADestroy @q
    EXEC @hr = sp_OADestroy @qOut
    EXEC @hr = sp_OADestroy @withPassword


END
GO