SQL Server Requires Chilkat v11.7.0+
SQL Server
Extract Text from a PDF that Forbids Copying
See more PDF Text Extraction Examples
Demonstrates a PDF that opens without a password but whose permissions do not allow copying text. The "text", "markdown", "search" and "tables" operations fail with that reason; "assess" always works. Loading the PDF with the OwnerPassword property set grants owner rights, and extraction works.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr 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 (owner password: owner-secret; copying not permitted) is available at no_copy_permission.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 opens without a password.
EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/no_copy_permission.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
-- Extracting text is refused, because the PDF's permissions do not allow copying.
EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
PRINT 'Extraction refused: ' + @sTmp0
END
-- "assess" reports only counts and ratings, never text, so it is always allowed.
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, 'StringOf', @sTmp0 OUT, 'quality.rating'
PRINT 'rating: ' + @sTmp0
-- Load with the owner password to get owner rights, which include copying.
EXEC sp_OASetProperty @pdf, 'OwnerPassword', 'owner-secret'
EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/no_copy_permission.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:
-- Extraction refused: ChilkatLog:
-- ExtractText:
-- ...
-- The PDF's permissions do not allow copying or extracting text (load it with the owner password to extract).
-- ...
-- rating: good
-- Visitor Leaflet
--
-- Welcome to the harbor lighthouse. The tower is open on weekends from July to September.
--
-- Please keep to the marked paths and do not climb on the breakwater.
--
-- The lantern room is closed in high winds.
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO