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

PDF Text Layouts: flow, lines and grid

See more PDF Text Extraction Examples

Demonstrates the "layout" option of the "text" operation. "flow" (the default) joins the lines of each paragraph and returns tables as tab-separated rows; "lines" keeps each visual line of the page on its own line; "grid" pads with spaces so that the horizontal positions are preserved, like a fixed-pitch rendering of the page.

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 PDF used in this example (a title, a paragraph, and a table with ruled cells) is available at warehouse_inventory.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/warehouse_inventory.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 @sb int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT

    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    --  ---- flow (the default) ----
    --  The lines of a paragraph are joined into one line.  A table becomes one line per row
    --  with the cells separated by tabs, followed by a blank line.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'layout', 'flow'
    EXEC sp_OAMethod @pdf, 'ExtractTextSb', @success OUT, 'text', @json, @sb, @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 @sb
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    PRINT '---- flow ----'
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    --  ---- lines ----
    --  Each visual line of the page is one output line.  A table row is its cells joined by single spaces.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'layout', 'lines'
    EXEC sp_OAMethod @sb, 'Clear', NULL
    EXEC sp_OAMethod @pdf, 'ExtractTextSb', @success OUT, 'text', @json, @sb, @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 @sb
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    PRINT '---- lines ----'
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    --  ---- grid ----
    --  Each line is padded with spaces so that the text sits at its horizontal position on the page.
    --  Table columns line up as they do on the page, which suits fixed-width parsing.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'layout', 'grid'
    EXEC sp_OAMethod @sb, 'Clear', NULL
    EXEC sp_OAMethod @pdf, 'ExtractTextSb', @success OUT, 'text', @json, @sb, @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 @sb
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    PRINT '---- grid ----'
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    --  Sample output (abbreviated; the tabs in the flow output are shown as -> here):

    --  ---- flow ----
    --  Warehouse Inventory Report
    --  
    --  Stock on hand at the Wheaton distribution center as of September 30, 2026. Quantities are counted units; the unit cost is the average landed cost in US dollars. Items at or below their reorder point are flagged in the status column.
    --  
    --  SKU->Description->Category->Qty on Hand->Unit Cost->Status
    --  WH-1001->Steel shelving unit, 5 tier->Storage->42->189.50->OK
    --  WH-1002->Pallet jack, 5500 lb->Equipment->7->412.00->Reorder
    --  WH-1003->Stretch wrap, 18 in roll->Packaging->640->9.75->OK
    --  ...
    --  WH-1012->Wire shelf liner, 48 in->Storage->24->6.15->Reorder
    --  
    --  Four items are at or below their reorder point and one item is out of stock. Purchase orders for the flagged items should be raised before the October cycle count.
    --  
    --  ---- lines ----
    --  Warehouse Inventory Report
    --  Stock on hand at the Wheaton distribution center as of September 30, 2026. Quantities are counted units; the
    --  unit cost is the average landed cost in US dollars. Items at or below their reorder point are flagged in the status
    --  column.
    --  SKU Description Category Qty on Hand Unit Cost Status
    --  WH-1001 Steel shelving unit, 5 tier Storage 42 189.50 OK
    --  WH-1002 Pallet jack, 5500 lb Equipment 7 412.00 Reorder
    --  WH-1003 Stretch wrap, 18 in roll Packaging 640 9.75 OK
    --  ...
    --  WH-1012 Wire shelf liner, 48 in Storage 24 6.15 Reorder
    --  Four items are at or below their reorder point and one item is out of stock. Purchase orders for the flagged items
    --  should be raised before the October cycle count.
    --  
    --  ---- grid ----
    --                           Warehouse Inventory Report
    --  Stock on hand at the Wheaton distribution center as of September 30, 2026. Quantities are counted units; the
    --  unit cost is the average landed cost in US dollars. Items at or below their reorder point are flagged in the status
    --  column.
    --  
    --  SKU         Description                       Category      Qty on Hand    Unit Cost   Status
    --  WH-1001     Steel shelving unit, 5 tier       Storage                 42       189.50  OK
    --  WH-1002     Pallet jack, 5500 lb              Equipment                7       412.00  Reorder
    --  WH-1003     Stretch wrap, 18 in roll          Packaging              640         9.75  OK
    --  ...
    --  WH-1012     Wire shelf liner, 48 in           Storage                 24         6.15  Reorder
    --  
    --  Four items are at or below their reorder point and one item is out of stock. Purchase orders for the flagged items
    --  should be raised before the October cycle count.

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


END
GO