SQL Server Requires Chilkat v11.7.0+
SQL Server
Extract the OCR Text Layer of a Scanned PDF
See more PDF Text Extraction Examples
Demonstrates a scanned page that carries an invisible OCR text layer over the page image. The quality report identifies the page's textSource as "ocrLayer". The "invisibleText" filter is "auto" by default, which uses the invisible text only on pages that have no visible text; "exclude" ignores it.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 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 (a page image with an invisible text layer, as an OCR program produces) is available at scanned_with_ocr.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/scanned_with_ocr.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 is a picture of a handwritten log with the OCR'd words drawn invisibly over it.
-- "auto" (the default) includes invisible text exactly in this case: on a page with no visible text.
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, 'quality.pages[0].textSource'
PRINT 'textSource: ' + @sTmp0
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pages[0].rating'
PRINT 'rating: ' + @sTmp0
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
PRINT @sTmp0
-- The quality report flags the page, because OCR text is only as good as the OCR was.
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pages[0].issues[0].code'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'quality.pages[0].issues[0].message'
PRINT 'issue: ' + @sTmp0 + ' - ' + @sTmp1
-- "exclude" ignores invisible text altogether: the page then has no text at all.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'invisibleText', 'exclude'
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, 'quality.pages[0].textSource'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'text'
PRINT 'With invisibleText excluded, textSource: ' + @sTmp0 + ', text: [' + @sTmp1 + ']'
-- Sample output:
-- textSource: ocrLayer
-- rating: good
-- Keeper's Log, 12 October 1902
--
-- Wind north-west, fresh. Lamp lit at 5.40 p.m. and burning well all night.
--
-- Schooner Mary Ann passed inbound at 9 p.m. showing proper lights.
--
-- Oil on hand: forty-one gallons. Cleaned the lens at dawn.
-- issue: ocrLayer - The text is an invisible OCR layer over an image
-- With invisibleText excluded, textSource: ocrLayer, text: []
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO