SQL Server Requires Chilkat v11.7.0+
SQL Server
Assess PDF Text Quality Before Extracting
See more PDF Text Extraction Examples
Demonstrates the "assess" operation, which returns only the quality report: whether the text can be trusted, which pages are scanned images that need OCR, and how each page's text was found. Use it to triage documents before extracting. (Chilkat does not perform OCR.)Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 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 (page 2 is a scanned image with no text) is available at mixed_text_image.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/mixed_text_image.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
-- "assess" computes the quality report without producing any text.
-- It is fast, and it is always allowed, even for an encrypted PDF that forbids copying.
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
-- The overall rating is the worst rating among the pages that have text:
-- "good", "fair", "poor", or "none" when there is no text at all.
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.rating'
PRINT 'Rating: ' + @sTmp0
-- Is the PDF tagged (does it have a structure tree)?
EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'quality.tagged'
PRINT 'Tagged: ' + @iTmp0
-- The pages that are images only and would need OCR, such as "2" or "4,9".
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pagesNeedingOcr'
PRINT 'Pages needing OCR: ' + @sTmp0
-- The per-page report.
DECLARE @j int
DECLARE @count_j int
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @count_i OUT, 'quality.pages'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jsonOut, 'I', @i
-- textSource is "text" (real text), "ocrLayer" (invisible text over an image),
-- "imageOnly" (needs OCR), "mixed", or "empty".
EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'quality.pages[i].page'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pages[i].rating'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'quality.pages[i].textSource'
PRINT 'Page ' + @iTmp0 + ': rating ' + @sTmp0 + ', textSource ' + @sTmp1
-- Each issue has a machine-readable code and a sentence.
SELECT @j = 0
EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @count_j OUT, 'quality.pages[i].issues'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @jsonOut, 'J', @j
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pages[i].issues[j].code'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'quality.pages[i].issues[j].message'
PRINT ' ' + @sTmp0 + ': ' + @sTmp1
SELECT @j = @j + 1
END
SELECT @i = @i + 1
END
-- Decide what to do with the document:
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.rating'
IF @sTmp0 = 'none'
BEGIN
PRINT 'No extractable text; the whole document needs OCR.'
END
ELSE
BEGIN
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pagesNeedingOcr'
IF @sTmp0 <> ''
BEGIN
PRINT 'Some pages need OCR; the remaining pages can be extracted with the text or markdown operation.'
END
ELSE
BEGIN
PRINT 'All pages can be extracted.'
END
END
-- Sample output:
-- Rating: good
-- Tagged: false
-- Pages needing OCR: 2
-- Page 1: rating good, textSource text
-- Page 2: rating none, textSource imageOnly
-- imageOnly: No text; page is a scanned image
-- Page 3: rating good, textSource text
-- Some pages need OCR; the remaining pages can be extracted with the text or markdown operation.
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO