Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.7.0+

Extract Text from a PDF

See more PDF Text Extraction Examples

Demonstrates the simplest way to extract the text of a PDF: the "text" operation with default options returns the text of all pages as flowing paragraphs, together with a quality report that says whether the text can be trusted.

Chilkat SQL Server Downloads

SQL Server
--
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 two-page PDF used in this example is available at two_page_memo.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/two_page_memo.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    --  The extraction options are passed in a JSON object.  An empty JSON object means:
    --  all pages, automatic reading order, flowing paragraphs, and the default clean-up.
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    --  The results are returned in a second JSON object.
    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    --  The first argument selects the operation.  "text" returns plain text.
    --  (The other operations are "markdown", "search", "tables" and "assess".)
    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

    --  The text of all pages is in the "text" member.  Each paragraph is one line,
    --  and pages are separated by a form feed character by default.
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT @sTmp0

    --  Every operation also returns a quality report.  A rating of "good" means the text
    --  can be trusted; "fair" and "poor" mean it should be reviewed, and "none" means the
    --  PDF has no extractable text (for example, a scanned image that needs OCR).

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.rating'
    PRINT 'Quality: ' + @sTmp0

    --  Sample output (the form feed between the pages is shown as \f):

    --  Riverside Community Garden: Season Summary
    --  
    --  The garden opened for its fourth season on the first Saturday in April. All forty-two plots were assigned by the end of the month, and a waiting list of eleven households was kept for the plots that became available over the summer.
    --  
    --  Rainfall was close to the ten-year average, so the shared water tanks were refilled only twice. The new drip lines along the north fence worked well and will be extended to the raised beds next spring.
    --  
    --  The harvest table donated just over six hundred pounds of produce to the food pantry on Elm Street. Tomatoes, squash and green beans made up most of the weight, with a surprising number of hot peppers in late August.
    --  \fPlans for Next Season
    --  
    --  The committee agreed to replace the tool shed roof before winter and to add a second compost bay. Both projects will be paid for from the plot fees already collected.
    --  
    --  Registration for returning gardeners opens on the first of February. New applicants will be taken from the waiting list in the order in which they signed up.
    --  
    --  Quality: good

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut


END
GO