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

Extract Text from Specific PDF Pages

See more PDF Text Extraction Examples

Demonstrates the "pages" option, which selects the pages to extract (a list of pages and ranges, or "first", "last", "odd", "even"), the "pageSeparator" option, and how to get the text of each page separately.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 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 four-page PDF used in this example is available at four_pages.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/four_pages.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

    --  Pages are numbered from 1.  The "pages" option can be "all" (the default), "first", "last",
    --  "odd", "even", or a list such as "1,3,5-9,12-".  Here we extract pages 2 and 3.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'pages', '2-3'

    --  By default the pages in the joined text are separated by a form feed character.
    --  Use any string instead:
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'pageSeparator', CHAR(10) + '----- next page -----' + CHAR(10)

    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

    PRINT ''

    --  The "pages" array has the text of each extracted page with its page number.
    --  This is the way to keep the text of each page separate.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'pages', '1,4'
    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

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @count_i OUT, 'pages'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jsonOut, 'I', @i

        EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'pages[i].page'

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'pages[i].text'
        PRINT 'Page ' + @iTmp0 + ': ' + @sTmp0
        SELECT @i = @i + 1
      END

    --  Sample output:

    --  Chapter Two: The Crossing
    --  
    --  This is page two of four. The crossing takes forty minutes in calm weather. Today the sea was flat and the gulls followed the boat the whole way.
    --  
    --  ----- next page -----
    --  Chapter Three: Arrival
    --  
    --  This is page three of four. The island harbor was busy with the morning catch being unloaded. The potatoes went straight up the hill to the store on a hand cart.
    --  
    --  
    --  Page 1: Chapter One: Departure
    --  
    --  This is page one of four. The ferry left the north pier at seven in the morning with eleven passengers, two dogs and a crate of seed potatoes for the island store.
    --  
    --  Page 4: Chapter Four: Return
    --  
    --  This is page four of four. The ferry turned round within the hour and sailed back with the mail, three passengers and an empty crate.

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


END
GO