SQL Server Requires Chilkat v11.7.0+
SQL Server
Remove a PDF's Old Revisions
See more PDF Optimization Examples
Demonstrates dropOldRevisions. A PDF that has been edited and saved several times carries every earlier revision: the superseded objects are still in the file, reachable through the older cross-reference sections. The audit reports them as "oldRevisions", and dropping them is often the single largest saving in a real document. Dropping them requires a full rewrite, which is why it invalidates existing digital signatures.Chilkat SQL Server Downloads
--
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 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
-- How much of this file is superseded revisions?
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, 'IntOf', @iTmp0 OUT, 'usage.oldRevisions.bytes'
PRINT 'oldRevisions: ' + @iTmp0
-- Drop them, and nothing else, so that the saving is attributable.
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'compressUncompressed', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'recompressFlate', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'replaceLegacyFilters', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'imagePredictors', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'dedupeStreams', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'dedupeObjects', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'removeUnreferenced', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'removeUnusedResources', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'removeUnusedLayers', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'coalesceContent', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'tidyContent', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'subsetFonts', 0
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'dropOldRevisions', 1
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_one_revision.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
-- The saving is an estimate: what the old revisions occupied is known exactly, but the writer
-- decides the final layout. Measure the written file for the real number.
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'bytesSaved'
PRINT 'bytesSaved (reported): ' + @iTmp0
EXEC sp_OAMethod @fac, 'FileSize', @iTmp0 OUT, 'qa_output/grand_canyon_one_revision.pdf'
PRINT 'written file: ' + @iTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @fac
END
GO