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

Where a PDF's Bytes Go: the Optimize Audit

See more PDF Optimization Examples

Demonstrates the "audit" operation, which measures the loaded PDF and changes nothing. The report says how large the file is, which PDF version it claims, whether it claims to be PDF/A, where its bytes go ("usage", as stored in the file, so compressed sizes), and how many bytes each lossless option would be expected to save ("estimatedSavings"). Use it to decide whether optimizing is worth the trouble, and which options matter for this file.

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
    DECLARE @iTmp0 int
    DECLARE @iTmp1 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 grand_canyon_hiking_guide.pdf

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

    EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/grand_canyon_hiking_guide.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    --  "audit" takes the same options as "optimize", so an empty JSON means the default options.
    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, 'Optimize', @success OUT, 'audit', @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, 'IntOf', @iTmp0 OUT, 'fileSize'
    PRINT 'fileSize: ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'pdfVersion'
    PRINT 'pdfVersion: ' + @sTmp0

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'pdfa'
    PRINT 'pdfa: ' + @sTmp0

    --  Where the bytes go.  Every category is present even when it is 0, and the sizes are the stored
    --  (compressed) sizes, so they add up to fileSize.  "overhead" is what is left: the cross-reference
    --  tables, the object headers and the white space between objects.

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.images.bytes'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp1 OUT, 'usage.images.count'

    PRINT 'images:        ' + @iTmp0 + ' in ' + @iTmp1 + ' objects'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.fonts.bytes'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp1 OUT, 'usage.fonts.count'

    PRINT 'fonts:         ' + @iTmp0 + ' in ' + @iTmp1 + ' objects'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.pageContent.bytes'
    PRINT 'pageContent:   ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.formXObjects.bytes'
    PRINT 'formXObjects:  ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.annotations.bytes'
    PRINT 'annotations:   ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.structureTree.bytes'
    PRINT 'structureTree: ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.metadata.bytes'
    PRINT 'metadata:      ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.embeddedFiles.bytes'
    PRINT 'embeddedFiles: ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.oldRevisions.bytes'
    PRINT 'oldRevisions:  ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'usage.overhead.bytes'
    PRINT 'overhead:      ' + @iTmp0

    --  What each option is expected to save.  An option that would save nothing is left out.

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'estimatedSavings.total'
    PRINT 'estimated total: ' + @iTmp0

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

    EXEC sp_OAMethod @jsonOut, 'ObjectOf2', @success OUT, 'estimatedSavings', @est
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jsonOut, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @est
        RETURN
      END

    DECLARE @i int
    SELECT @i = 0
    DECLARE @numEst int
    EXEC sp_OAGetProperty @est, 'Size', @numEst OUT
    WHILE @i < @numEst
      BEGIN
        DECLARE @optionName nvarchar(4000)
        EXEC sp_OAMethod @est, 'NameAt', @optionName OUT, @i


        EXEC sp_OAMethod @est, 'IntOf', @iTmp0 OUT, @optionName
        PRINT '   ' + @optionName + ': ' + @iTmp0
        SELECT @i = @i + 1
      END

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


END
GO