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

Extract PDF to Markdown

See more PDF Text Extraction Examples

Demonstrates the "markdown" operation, which returns the PDF's content as Markdown: headings (inferred from the font sizes when the PDF is not tagged), bold and italic, bullet and numbered lists, links, and ruled tables. Markdown output is well suited to AI and search pipelines.

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 markdown_features.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/markdown_features.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 "markdown" operation uses the same options as "text" (pages, reading order, filters, clean-up)
    --  plus its own options (see the pdf_extract_markdown_options example).  The defaults convert everything.
    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

    --  The Markdown of all pages is in the "markdown" member.
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'markdown'
    PRINT @sTmp0

    --  The quality report says where the headings came from: "structure" for a tagged PDF,
    --  "inferred" when they were recognized from the font size and weight, or "none".

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pages[0].structure.headings'
    PRINT 'Headings: ' + @sTmp0

    --  Sample output:

    --  <!-- page 1 -->
    --  
    --  # Field Guide to Backyard Birds
    --  
    --  This short guide covers the birds most often seen at a suburban feeder in the eastern United States. The **bold** names are species you can expect in every season; the *italic* names are winter visitors only.
    --  
    --  ## What to Watch For
    --  
    --  - Size relative to a sparrow or a robin
    --  - Beak shape: thick for seed eaters, thin for insect eaters
    --  - Wing bars and tail markings seen in flight
    --  
    --  ## Getting Started
    --  
    --  1. Hang the feeder where it can be seen from a window.
    --  2. Fill it with black oil sunflower seed, which almost every species takes.
    --  3. Keep a notebook by the window and write down the date of each first sighting.
    --  
    --  For regional checklists and recordings of each song, see the [online bird atlas](https://www.example.com/birds).
    --  
    --  ## Common Visitors
    --  
    --  | Species | Size | Season | Preferred Food |
    --  | --- | --- | --- | --- |
    --  | Northern Cardinal | Robin | All year | Sunflower seed |
    --  | Black-capped Chickadee | Sparrow | All year | Sunflower seed, suet |
    --  | Dark-eyed Junco | Sparrow | Winter | Millet on the ground |
    --  | Downy Woodpecker | Sparrow | All year | Suet |
    --  
    --  Headings: inferred

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


END
GO