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

PDF to Markdown Options: Page Breaks, Images, Tables, Emphasis, Heading Levels

See more PDF Text Extraction Examples

Demonstrates the options of the "markdown" operation: pageBreaks (comment, rule or none), images (placeholder or none), tables (ruled or none), emphasis, and maxHeadingLevel.

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 two-page PDF used in this example is available at markdown_options.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_options.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 @sbMarkdown int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMarkdown OUT

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

    --  First with the default options.
    EXEC sp_OAMethod @pdf, 'ExtractTextSb', @success OUT, 'markdown', @json, @sbMarkdown, @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 @sbMarkdown
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    PRINT '---- default options ----'
    EXEC sp_OAMethod @sbMarkdown, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    --  Now change the options:

    --  pageBreaks: "comment" (the default) separates pages with <!-- page N -->,
    --  "rule" separates them with a horizontal rule (---), and "none" runs the pages together.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'pageBreaks', 'rule'

    --  images: "placeholder" (the default) marks where each image sits with ![image](page-1-image-1);
    --  "none" leaves images out.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'images', 'none'

    --  tables: "ruled" (the default) converts tables that have drawn cell borders into Markdown tables;
    --  "none" returns their cells as plain text.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'tables', 'none'

    --  emphasis: false leaves out the ** and * markers for bold and italic.
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'emphasis', 0

    --  maxHeadingLevel: inferred headings deeper than this level are folded into this level.
    --  The PDF has three heading levels; with maxHeadingLevel 2 the third level becomes ##.
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'maxHeadingLevel', 2

    EXEC sp_OAMethod @sbMarkdown, 'Clear', NULL
    EXEC sp_OAMethod @pdf, 'ExtractTextSb', @success OUT, 'markdown', @json, @sbMarkdown, @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 @sbMarkdown
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    PRINT '---- custom options ----'
    EXEC sp_OAMethod @sbMarkdown, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    --  Sample output:

    --  ---- default options ----
    --  <!-- page 1 -->
    --  
    --  # Community Garden Handbook
    --  
    --  ## Plots
    --  
    --  ### Plot Sizes
    --  
    --  Plots come in two sizes. A **full plot** is twenty feet by ten feet; a **half plot** is ten feet square. New gardeners are *strongly encouraged* to start with a half plot.
    --  
    --  ![image](page-1-image-1)
    --  
    --  ### Plot Fees
    --  
    --  | Plot | Size | Annual Fee |
    --  | --- | --- | --- |
    --  | Full | 20 x 10 ft | $60 |
    --  | Half | 10 x 10 ft | $35 |
    --  
    --  <!-- page 2 -->
    --  
    --  ## Watering
    --  
    --  ### Schedule
    --  
    --  The shared tanks are filled on Monday and Thursday mornings. Please water by hand from the tanks rather than running a hose from the street tap, which is metered.
    --  
    --  ### Dry Spells
    --  
    --  During a declared drought the committee may limit watering to **two days a week**. Notices are posted on the shed door and sent by e-mail.
    --  
    --  ---- custom options ----
    --  # Community Garden Handbook
    --  
    --  ## Plots
    --  
    --  ## Plot Sizes
    --  
    --  Plots come in two sizes. A full plot is twenty feet by ten feet; a half plot is ten feet square. New gardeners are strongly encouraged to start with a half plot.
    --  
    --  ## Plot Fees
    --  
    --  Plot Size Annual Fee
    --  
    --  Full 20 x 10 ft $60
    --  
    --  Half 10 x 10 ft $35
    --  
    --  ---
    --  
    --  ## Watering
    --  
    --  ## Schedule
    --  
    --  The shared tanks are filled on Monday and Thursday mornings. Please water by hand from the tanks rather than running a hose from the street tap, which is metered.
    --  
    --  ## Dry Spells
    --  
    --  During a declared drought the committee may limit watering to two days a week. Notices are posted on the shed door and sent by e-mail.

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


END
GO