SQL Server Requires Chilkat v11.7.0+
SQL Server
Choose Which PDF Optimizations to Apply
See more PDF Optimization Examples
Demonstrates turning individual optimizations off. Every option is on by default except the two reserved ones; setting one to false leaves that part of the file alone. Here the old revisions are kept (so the output is an incremental update that leaves the original bytes untouched) and the fonts are not subset, which is what a workflow that will later fill in form fields or replace text needs. The "actions" in the report name only the options that did something.Chilkat SQL Server Downloads
-- 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
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
-- The options, all boolean, all true by default:
-- compressUncompressed compress streams stored with no filter
-- recompressFlate deflate again at the chosen level
-- replaceLegacyFilters re-encode LZW, RunLength, ASCIIHex and ASCII85 streams as Flate
-- imagePredictors apply a PNG predictor where it makes an image stream smaller
-- dedupeStreams merge streams whose contents are identical
-- dedupeObjects merge objects whose contents are identical
-- removeUnreferenced drop objects nothing refers to
-- removeUnusedResources drop resource entries no page uses
-- removeUnusedLayers drop optional-content groups nothing uses
-- coalesceContent join a page's content streams into one
-- tidyContent remove redundant operators and white space from page content
-- subsetFonts keep only the glyphs the document uses
-- dropOldRevisions drop superseded revisions (a full rewrite)
--
-- mergeFontSubsets and compactType1Fonts are reserved for a later release and are reported in
-- "skipped" when requested. See the "Why an Optimization Was Skipped" example.
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'subsetFonts', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'dropOldRevisions', 0
DECLARE @jsonOut int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT
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/grand_canyon_some_options.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
-- Only the options that actually did something appear under "actions", each with the number of
-- objects it affected and the bytes it saved.
DECLARE @actions int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @actions OUT
EXEC sp_OAMethod @jsonOut, 'ObjectOf2', @success OUT, 'actions', @actions
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 @actions
RETURN
END
DECLARE @oneAction int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @oneAction OUT
DECLARE @i int
SELECT @i = 0
DECLARE @numActions int
EXEC sp_OAGetProperty @actions, 'Size', @numActions OUT
WHILE @i < @numActions
BEGIN
DECLARE @actionName nvarchar(4000)
EXEC sp_OAMethod @actions, 'NameAt', @actionName OUT, @i
EXEC sp_OAMethod @actions, 'ObjectOf2', @success OUT, @actionName, @oneAction
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @actions, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @actions
EXEC @hr = sp_OADestroy @oneAction
RETURN
END
EXEC sp_OAMethod @oneAction, 'IntOf', @iTmp0 OUT, 'bytesSaved'
EXEC sp_OAMethod @oneAction, 'IntOf', @iTmp1 OUT, 'count'
PRINT @actionName + ': ' + @iTmp0 + ' bytes, ' + @iTmp1 + ' objects'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @actions
EXEC @hr = sp_OADestroy @oneAction
END
GO