SelectPdf for .NET 10 - Convert Pdf to Excel - C# / .NET 10 Sample

This sample shows how to use the SelectPdf pdf to Excel converter to convert a pdf document to an Excel workbook. The tables of the pages become worksheets, and numbers and dates become typed cell values, so formulas and sorting work on them. The conversion runs in-process; no engine is launched.

The sample converts the following pdf, which Microsoft Excel saved from the Excel to Pdf sample's workbook, or a pdf you upload:
Test pdf document

Pdf document (optional):

Worksheets:


Culture of the numbers and dates (empty for the current one, e.g. en-US, de-DE):


Pages (empty for all, e.g. 1,3-5):


Text outside the tables
Images

Sample code · C#
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class PdfToExcelModel : PageModel
    {
        public void OnGet()
        {
            DdlSheets = "PerTable";
            TxtCulture = "";
            TxtPageRanges = "";
            ChkTextOutsideTables = true;
            ChkImages = true;
        }

        // An uploaded pdf, or nothing - the shipped files/workbook.pdf is converted then.
        [BindProperty]
        public IFormFile FilePdf { get; set; }

        [BindProperty]
        public string DdlSheets { get; set; }

        public List<SelectListItem> SheetModes { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "PerTable", Text = "A worksheet per table" },
            new SelectListItem { Value = "PerPage", Text = "A worksheet per page" },
        };

        [BindProperty]
        public string TxtCulture { get; set; }

        [BindProperty]
        public string TxtPageRanges { get; set; }

        [BindProperty]
        public bool ChkTextOutsideTables { get; set; }

        [BindProperty]
        public bool ChkImages { get; set; }

        public IActionResult OnPost()
        {
            // read parameters from the webpage
            PdfToExcelSheets sheets = SampleForm.ParseEnum(DdlSheets, PdfToExcelSheets.PerTable);

            // instantiate a pdf to excel converter object
            PdfToExcel converter = new PdfToExcel();

            // set converter options: a worksheet per table or per page, the culture the numbers and
            // dates of the pdf are read in (empty for the current one) - cells holding them get typed
            // values, so formulas and sorting work on them - the pages converted (empty for all),
            // and the text around the tables and the pictures
            converter.Options.Sheets = sheets;
            converter.Options.Culture = (TxtCulture ?? string.Empty).Trim();
            converter.Options.PageRanges = TxtPageRanges ?? string.Empty;
            converter.Options.TextOutsideTables = ChkTextOutsideTables;
            converter.Options.Images = ChkImages;

            // convert the uploaded pdf, or the sample one shipped with the application
            byte[] workbook;
            if (FilePdf != null && FilePdf.Length > 0)
            {
                using (Stream uploaded = FilePdf.OpenReadStream())
                {
                    workbook = converter.ConvertStream(uploaded);
                }
            }
            else
            {
                string pdfFile = Path.Combine(Startup.ServerPath, "wwwroot", "files", "workbook.pdf");
                workbook = converter.ConvertFile(pdfFile);
            }

            // return the converted workbook
            FileResult fileResult = new FileContentResult(workbook,
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            fileResult.FileDownloadName = "Workbook.xlsx";
            return fileResult;
        }
    }
}