PDF to Markdown Extraction

29 Jun 202611 minutes to read

Markdown is a lightweight markup language that adds formatting elements to plain text documents. The Syncfusion® Smart Data Extractor library extracts structured information from PDF documents and scanned images, and outputs the content as Markdown (MD). It analyzes text blocks, tables, headers, and form fields to preserve layout and formatting.

Assemblies and NuGet packages required

Refer to the following links for assemblies and NuGet packages required based on platforms to Extract data as Markdown file using the .NET Word Library (DocIO).

Extract Data as Markdown from PDF or Image

To extract form fields across a PDF document using the ExtractDataAsMarkdown method of the DataExtractor class, refer to the following code example:

using System.IO;
using Syncfusion.SmartDataExtractor;
using System.Text;

//Open the input PDF file as a stream.
using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
    //Initialize the Data Extractor.
    DataExtractor extractor = new DataExtractor();
    //Extract data as Markdown.
    string data = extractor.ExtractDataAsMarkdown(stream);
    //Save the extracted Markdown data into an output file.
    File.WriteAllText("Output.md", data, Encoding.UTF8);
}
using System.IO;
using Syncfusion.SmartDataExtractor;
using System.Text;

//Open the input PDF file as a stream.
using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
    //Initialize the Data Extractor.
    DataExtractor extractor = new DataExtractor();
    //Extract data as Markdown.
    string data = extractor.ExtractDataAsMarkdown(stream);
    //Save the extracted Markdown data into an output file.
    File.WriteAllText("Output.md", data, Encoding.UTF8);
}

NOTE

If you want to extract data from an image instead of a PDF, replace the input stream with the image file (for example, Input.jpg or Input.png). The rest of the code remains unchanged.

You can download a complete working sample from GitHub.

Extract a specific page to Markdown

The following code demonstrates how to use the ExtractDataAsMarkdown method of the DataExtractor class to extract content from a selected page in a PDF and save it as a Markdown file by specifying its page index.

using System.IO;
using Syncfusion.SmartDataExtractor;
using System.Text;

//Open the input PDF file as a stream.
using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
    //Initialize the Data Extractor.
    DataExtractor extractor = new DataExtractor();
    //Set the page index for extraction (example: page 2).
    extractor.PageRange = new int[,] { { 2, 2 } };
    //Extract data as Markdown using the API.
    string data = extractor.ExtractDataAsMarkdown(stream);
    //Save the extracted Markdown data into an output file.
    File.WriteAllText("Output.md", data, Encoding.UTF8);
}
using System.IO;
using Syncfusion.SmartDataExtractor;
using System.Text;

//Open the input PDF file as a stream.
using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
    //Initialize the Data Extractor.
    DataExtractor extractor = new DataExtractor();
    //Set the page index for extraction (example: page 2).
    extractor.PageRange = new int[,] { { 2, 2 } };
    //Extract data as Markdown using the API.
    string data = extractor.ExtractDataAsMarkdown(stream);
    //Save the extracted Markdown data into an output file.
    File.WriteAllText("Output.md", data, Encoding.UTF8);
}

Extract a range of pages to Markdown

The following code demonstrates how to use the ExtractDataAsMarkdown method of the DataExtractor class to extract content from a range of pages in a PDF and save it as a Markdown file by specifying the page range.

using System.IO;
using Syncfusion.SmartDataExtractor;
using System.Text;

//Open the input PDF file as a stream.
using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
    //Initialize the Data Extractor.
    DataExtractor extractor = new DataExtractor();
    //Set the page range for extraction (pages 1 to 3).
    extractor.PageRange = new int[,] { { 1, 3 } };
    //Extract data as Markdown using the API.
    string data = extractor.ExtractDataAsMarkdown(stream);
    //Save the extracted Markdown data into an output file.
    File.WriteAllText("Output.md", data, Encoding.UTF8);
}
using System.IO;
using Syncfusion.SmartDataExtractor;
using System.Text;

//Open the input PDF file as a stream.
using (FileStream stream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
    //Initialize the Data Extractor.
    DataExtractor extractor = new DataExtractor();
    //Set the page range for extraction (pages 1 to 3).
    extractor.PageRange = new int[,] { { 1, 3 } };
    //Extract data as Markdown using the API.
    string data = extractor.ExtractDataAsMarkdown(stream);
    //Save the extracted Markdown data into an output file.
    File.WriteAllText("Output.md", data, Encoding.UTF8);
}

Customize image saving

The ImageNodeVisited event in the SaveOptions class (from the Syncfusion® DocIO library, used within Smart Data Extractor) allows control over how images are handled when generating a Markdown string. With this event, you can:

  • Customize image names and storage paths, and save images externally using a FileStream.
  • Replace Base64 content with a file path or URL for optimized storage and cloud reference.
  • Generate a basic inbuilt report as a Markdown string, which can be directly consumed by LLMs or stored for further processing.

Extract Markdown with external image saving

The following code shows how to use the ExtractDataAsMarkdown method of the DataExtractor class with the ImageNodeVisited event to customize image saving while exporting PDF or image files as Markdown.

using Syncfusion.Office.Markdown;
using Syncfusion.SmartDataExtractor;

//Open the input PDF or Image file as a stream.
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
    //Initialize the Data Extractor.
    DataExtractor extractor = new DataExtractor();
    //Hook the event to customize image handling.
    extractor.SaveOptions.ImageNodeVisited += SaveImage;
    //Extract Markdown content as string.
    string data = extractor.ExtractDataAsMarkdown(inputStream);
    //Save the extracted Markdown data into an output file.
    File.WriteAllText("DataToMarkdown.md", data);
}
using Syncfusion.Office.Markdown;
using Syncfusion.SmartDataExtractor;

//Open the input PDF or Image file as a stream.
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
    //Initialize the Data Extractor.
    DataExtractor extractor = new DataExtractor();
    //Hook the event to customize image handling.
    extractor.SaveOptions.ImageNodeVisited += SaveImage;
    //Extract Markdown content as string.
    string data = extractor.ExtractDataAsMarkdown(inputStream);
    //Save the extracted Markdown data into an output file.
    File.WriteAllText("DataToMarkdown.md", data);
}

The following code shows how to implement the event handler to customize the image path and save images externally.

//Event handler to save images externally
static void SaveImage(object sender, MdImageNodeVisitedEventArgs args)
{
    //Define output image path (customize naming logic as needed)
    string imagePath = @"D:\Temp\Image1.png";
    //Save the image stream to file
    using (FileStream fileStreamOutput = File.Create(imagePath))
    {
        args.ImageStream.CopyTo(fileStreamOutput);
    }
    //Set the URI to be used in the Markdown output
    args.Uri = imagePath;
}
//Event handler to save images externally
static void SaveImage(object sender, MdImageNodeVisitedEventArgs args)
{
    //Define output image path (customize naming logic as needed)
    string imagePath = @"D:\Temp\Image1.png";
    //Save the image stream to file
    using (FileStream fileStreamOutput = File.Create(imagePath))
    {
        args.ImageStream.CopyTo(fileStreamOutput);
    }
    //Set the URI to be used in the Markdown output
    args.Uri = imagePath;
}

PDF to Markdown Preservation Mapping

This section explains how common PDF elements are converted and preserved in Markdown format, ensuring that document structure and formatting remain consistent during the PDF to Markdown conversion process.

PDF Elements Preservation in Markdown
Header, Paragraph Title, Document Title Headings (H2)
Paragraph Paragraph
Image Image (base64 string)
Table Table
Text Inline Styles Bold and Italic
Link text without title text Links
Code blocks, Footer, Page Number, List, Block quotes, Subscript, Superscript Text