Extract data as Excel from PDF or Image

26 Nov 20266 minutes to read

The Smart Data Extractor enables you to process PDF documents or scanned images and export the structured content as Excel (XLSX DOM).

This section covers two scenarios:

  • Extracting data as Excel from a PDF document.
  • Extracting data as Excel from an image.

Extract Data as Excel from PDF

To extract structured data from a PDF document using the ExtractDataAsExcel method of the DataExtractor class, refer to the following code example:

using Syncfusion.SmartDataExtractor;
using Syncfusion.XlsIO;

//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 Excel.
    IWorkbook excel = extractor.ExtractDataAsExcel(stream);
    //Save the extracted Excel data into an output file.
    excel.SaveAs("Output.xlsx");
    excel.Close();
}

You can download a complete working sample from GitHub.

Extract Data as Excel from Image

To extract structured data from an image file using the ExtractDataAsExcel method of the DataExtractor class, refer to the following code example:

using Syncfusion.SmartDataExtractor;
using Syncfusion.XlsIO;

//Open the input image file as a stream.
using (FileStream stream = new FileStream("Input.png", FileMode.Open, FileAccess.Read))
{
    //Initialize the Data Extractor.
    DataExtractor extractor = new DataExtractor();
    //Extract data as Excel.
    IWorkbook excel = extractor.ExtractDataAsExcel(stream);
    //Save the extracted Excel data into an output file.
    excel.SaveAs("Output.xlsx");
    excel.Close();
}

Convert a range of PDF pages to Excel

To convert a specific range of PDF pages from the PDF document into Excel by using the ExtractDataAsExcel method of the DataExtractor class, refer to the following code example:

using System.IO;
using Syncfusion.SmartDataExtractor;
using Syncfusion.XlsIO;

//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 conversion (example: pages 2 to 4).
    extractor.PageRange = new int[,] { { 2, 4 } };
    //Extract data from the selected pages as Excel.
    IWorkbook excel = extractor.ExtractDataAsExcel(stream);
    //Save the extracted Excel data into an output file.
    excel.SaveAs("Output.xlsx");
    excel.Close();
}
using System.IO;
using Syncfusion.SmartDataExtractor;
using Syncfusion.XlsIO;

//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 conversion (example: pages 2 to 4).
    extractor.PageRange = new int[,] { { 2, 4 } };
    //Extract data from the selected pages as Excel.
    IWorkbook excel = extractor.ExtractDataAsExcel(stream);
    //Save the extracted Excel data into an output file.
    excel.SaveAs("Output.xlsx");
    excel.Close();
}

Note: If you want to convert 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

Supported and Unsupported PDF Elements

The following table lists the PDF elements and their preservation details in the Excel

PDF Elements Supported
Title, Text Yes
Image Yes
Table Yes
Nested Table No
Text Inline Styles Yes (Bold and Italic)
Subscript, Superscript Yes (Searchable PDF documents only)
Underline, Strikethrough No
List No (Converted as line-by-line text)
Charts and Barcodes Yes (Preserved as images)
Code blocks, Header, Footer, Page Number Yes (Preserved as text)
Link No (Preserved as plain text)
Text color and Background color No
Logo in page header or footer No