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

Extract One Section of a PDF by its Heading

See more PDF Text Extraction Examples

Demonstrates "labelValue": "section", which returns everything from a heading up to the next heading of the same or higher level. With the "markdown" operation the section keeps its own sub-headings.

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 is available at sectioned_doc.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/sectioned_doc.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

    --  The document has the sections Introduction, Opening Hours, Terms and Conditions (with the
    --  sub-sections Admission, Safety and Liability) and Contact.  Get the Terms and Conditions:
    --  everything after that heading up to the next heading of the same level (Contact).  The heading
    --  itself is not part of the value; labelMatches[0].label has it.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'label', 'Terms and Conditions'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labelValue', 'section'

    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'markdown', @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, 'markdown'
    PRINT @sTmp0

    --  The same with the "text" operation gives the section as plain text.
    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 '---- as text ----'
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT @sTmp0

    --  Sample output:

    --  ### Admission
    --  
    --  Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
    --  
    --  ### Safety
    --  
    --  No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
    --  
    --  ### Liability
    --  
    --  Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.
    --  ---- as text ----
    --  Admission
    --  
    --  Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
    --  
    --  Safety
    --  
    --  No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
    --  
    --  Liability
    --  
    --  Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.

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


END
GO