SQL Server Requires Chilkat v11.7.0+
SQL Server
Clipped, Off-Page and White-on-White Text in PDF Extraction
See more PDF Text Extraction Examples
Demonstrates the "clippedText", "offPageText" and "obscuredText" filters. Text that a reader never sees - hidden by a clipping path, outside the crop box, or drawn in the same color as the box beneath it - is excluded by default, and the quality report notes what was skipped. Each kind can be included.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 is available at hidden_text.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/hidden_text.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
DECLARE @jsonOut int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT
-- The page has a visible paragraph plus three hidden lines: one drawn outside its clipping
-- rectangle, one below the crop box, and one in white on a white box.
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
PRINT '---- default ----'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
PRINT @sTmp0
-- The quality report says what was left out.
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @count_i OUT, 'quality.pages[0].issues'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jsonOut, 'I', @i
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pages[0].issues[i].code'
PRINT 'issue: ' + @sTmp0
SELECT @i = @i + 1
END
-- Include everything.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'clippedText', 'include'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'offPageText', 'include'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'obscuredText', 'include'
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
PRINT '---- clippedText, offPageText, obscuredText: include ----'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
PRINT @sTmp0
-- Sample output:
-- ---- default ----
-- Visible Text
--
-- This paragraph is visible. The page also carries three kinds of text a reader never sees.
--
-- End of the visible text.
-- issue: obscuredTextSkipped
-- issue: clippedTextSkipped
-- ---- clippedText, offPageText, obscuredText: include ----
-- Visible Text
--
-- This paragraph is visible. The page also carries three kinds of text a reader never sees.
--
-- CLIPPED: this line is drawn outside its clipping rectangle.
--
-- OBSCURED: this line is white on a white box.
--
-- End of the visible text.
--
-- OFFPAGE: this line sits below the crop box.
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO