Sample code for 30+ languages & platforms
Unicode C 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 Unicode C Downloads

Unicode C
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;

    success = FALSE;

    //  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 = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/code_block.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        return;
    }

    json = CkJsonObjectW_Create();
    jsonOut = CkJsonObjectW_Create();

    success = CkPdfW_ExtractText(pdf,L"markdown",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"---- default (code: true) ----\n");
    wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"markdown"));

    CkJsonObjectW_UpdateBool(json,L"code",FALSE);
    success = CkPdfW_ExtractText(pdf,L"markdown",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"---- code: false ----\n");
    wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"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.


    CkPdfW_Dispose(pdf);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);

    }