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; } } }