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

Extract Text from a Two-Column PDF

See more PDF Text Extraction Examples

Demonstrates how multi-column pages are read. By default the columns are detected and each column is read top to bottom before the next ("columns": "auto"); "columns": "none" reads the full page width line by line, which interleaves the columns. The quality report tells how many columns were found.

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 two-column PDF used in this example is available at two_column_article.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_column_article.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

    --  With the default options the columns are detected, and the text of the left column
    --  comes before the text of the right column.
    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, 'IntOf', @iTmp0 OUT, 'quality.pages[0].columns'
    PRINT 'Columns detected: ' + @iTmp0

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pages[0].readingOrderSource'
    PRINT 'Reading order: ' + @sTmp0
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT @sTmp0

    --  "none" switches column detection off: the page is read across its full width, line by line,
    --  so each output line holds a line of the left column followed by a line of the right column.
    --  This is rarely wanted, but it shows what the detection does.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'columns', 'none'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'layout', 'lines'
    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 '---- columns: none ----'
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT @sTmp0

    --  Sample output (abbreviated):

    --  Columns detected: 2
    --  Reading order: visual
    --  The Harbor Lighthouse, Season Two
    --  
    --  The harbor lighthouse was built in 1874 to mark the entrance to the inner channel. It operated continuously until 1968, ...
    --  
    --  The restoration society took ownership in 2021 with three goals: ...
    --  ...
    --  The lantern room frame was stripped, primed and repainted in the original dark green. ...
    --  ...
    --  ---- columns: none ----
    --  The Harbor Lighthouse, Season Two
    --  The harbor lighthouse was built in 1874 to The lantern room frame was stripped, primed
    --  mark the entrance to the inner channel. It and repainted in the original dark green. New
    --  ...

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


END
GO