How can I help you?
PDF to Markdown Extraction
25 May 20267 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);
}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 |