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

Code Blocks and Code Spans in PDF to Markdown

See more PDF Text Extraction Examples

Demonstrates how monospaced text is converted by the "markdown" operation: a monospaced block becomes a fenced code block and a monospaced run inside a sentence becomes a code span. Set "code" to false to treat it as ordinary text.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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 code_block.pdf

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/code_block.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckExtractText(pdf,"markdown",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "---- default (code: true) ----"
    Debug CkJsonObject::ckStringOf(jsonOut,"markdown")

    CkJsonObject::ckUpdateBool(json,"code",0)
    success = CkPdf::ckExtractText(pdf,"markdown",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "---- code: false ----"
    Debug CkJsonObject::ckStringOf(jsonOut,"markdown")

    ;  Sample output:

    ;  ---- default (code: true) ----
    ;  <!-- page 1 -->
    ;  
    ;  # Extracting Text
    ;  
    ;  Load the PDF and call `ExtractText` with the operation and an options object. The result is returned as JSON:
    ;  
    ;  ```
    ;  new Pdf pdf;
    ;  success = pdf.LoadFile("report.pdf");
    ;  new JsonObject json;
    ;  new JsonObject jsonOut;
    ;  success = pdf.ExtractText("text",json,jsonOut);
    ;  println jsonOut.StringOf("text");
    ;  ```
    ;  
    ;  An empty options object extracts every page with the default clean-up. Set `pages` to limit the extraction.
    ;  ---- code: false ----
    ;  <!-- page 1 -->
    ;  
    ;  # Extracting Text
    ;  
    ;  Load the PDF and call ExtractText with the operation and an options object. The result is returned as JSON:
    ;  
    ;  new Pdf pdf; success = pdf.LoadFile("report.pdf"); new JsonObject json; new JsonObject jsonOut; success = pdf.ExtractText("text",json,jsonOut); println jsonOut.StringOf("text");
    ;  
    ;  An empty options object extracts every page with the default clean-up. Set pages to limit the extraction.


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure