Articles in this section
Category / Section

How to import a PDF document into the ASP.NET Core Spreadsheet?

7 mins read

This knowledge base article explains how to import a PDF document into the ASP.NET Core Spreadsheet. At present, there is no built-in option to import a PDF document into the Spreadsheet control. But we have achieved this by converting the uploaded PDF document into an Excel file using Tabula and the Syncfusion XLSIO library. And load the converted Excel file into the Spreadsheet using openFromJson method in the Uploader control success event.

[CSHTML]

@{
   var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "Home/SaveFile"};
}

<div>
   <ejs-uploader id="UploadFiles" success="onUploadSuccess" asyncSettings="@asyncSettings">
   </ejs-uploader>
</div>

<ejs-spreadsheet id="spreadsheet" openUrl="Home/Open" saveUrl="Home/Save">
</ejs-spreadsheet>

<script>

   function onUploadSuccess(args) {
       if (args.operation == "upload") {
           var spreadsheet = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet');
           fetch('/Home/LoadExcel', {
               method: 'POST',
               headers: {
                   'Content-Type': 'application/json',
               },
               body: JSON.stringify({ Name: args.file.name }),
           })
               .then((response) => response.json())
               .then((data) => {
                   console.log(data);
                   // Load the retrieved JSON from server into a spreadsheet.
                   spreadsheet.openFromJson({ file: data }); 
               })
       }
   }
</script>

[CONTROLLER]

public IActionResult Open(IFormCollection openRequest)
       {
           OpenRequest open = new OpenRequest();
           open.File = openRequest.Files[0];
           return Content(Workbook.Open(open));
       }

       public IActionResult LoadExcel([FromBody] FileOptions file)
       {
           csvName = file.Name.Split('.')[0];
           ExcelEngine excelEngine = new ExcelEngine();
           IApplication application = excelEngine.Excel;
           OpenRequest open = new OpenRequest();
           string filePath = Startup._env.WebRootPath.ToString() + "\\" + csvName + ".csv";
           FileStream inputStream = new FileStream(filePath, FileMode.Open);
           IFormFile formFile = new FormFile(inputStream, 0, inputStream.Length, "", "Sample" + ".xls");             // Converting MemoryStream to IFormFile 
           open.File = formFile;
           var content = Workbook.Open(open);
           inputStream.Close();
           return Content(content);
       }

       private IActionResult PdfToExcel(string outputpath, string fileName)
       {
           csvName = fileName.Split('.')[0];
           ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\Java\jdk-10\bin\java.exe");
           startInfo.WindowStyle = ProcessWindowStyle.Hidden;
           //Sets the working directory
           startInfo.WorkingDirectory = outputpath;
           //Using the java dependencies to create csv file
           startInfo.Arguments = "-jar tabula-1.0.2-jar-with-dependencies.jar -p all -o " + csvName + ".csv " + fileName;
           Process currentProcess = Process.Start(startInfo);
           currentProcess.WaitForExit();
           string[] files = Directory.GetFiles(outputpath, csvName + ".csv");
           return null;
       }


       [AcceptVerbs("Post")]
       [HttpPost]
       public void SaveFile(IList<IFormFile> UploadFiles)
       {
             long size = 0;
           try
           {
               foreach (var file in UploadFiles)
               {
                   var filename = ContentDispositionHeaderValue
                                   .Parse(file.ContentDisposition)
                                   .FileName
                                   .Trim('"');
                    filename = _hostingEnvironment.WebRootPath + $@"\{filename}";
                   size += file.Length;
                   if (!System.IO.File.Exists(filename))
                   {
                       using (FileStream fs = System.IO.File.Create(filename))
                       {
                            file.CopyTo(fs);
                           fs.Close();
                           PdfToExcel(_hostingEnvironment.WebRootPath, UploadFiles[0].FileName);
                       }
                   }
                   else
                   {
                       using (FileStream fs = System.IO.File.Open(filename, FileMode.Append))
                       {
                           fs.Close();
                           PdfToExcel(_hostingEnvironment.WebRootPath, UploadFiles[0].FileName);
                       }
                   }
               }
           }
           catch (Exception e)
           {
               Response.Clear();
               Response.StatusCode = 204;
               Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
               Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
           }
       }

Sample Link:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/core-sample-pdf-load1793936168

Output:

Open-pdf.gif

For further information about this, see the demo and KB article that follows.

Knowledge Base:

https://support.syncfusion.com/kb/article/8603/how-to-convert-tables-in-pdf-document-to-excel-file

Demo:

https://ej2.syncfusion.com/aspnetcore/Uploader/DefaultFunctionalities#/material3

https://ej2.syncfusion.com/aspnetcore/Spreadsheet/DefaultFunctionalities#/material3

Documentation:

https://ej2.syncfusion.com/aspnetcore/documentation/spreadsheet/open-save

Conclusion

I hope you enjoyed learning about how to import a PDF document into the ASP.NET Core Spreadsheet.

You can refer to our ASP.NET Core Spreadsheet feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET Core Spreadsheet documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our ASP.NET Core Spreadsheet and other ASP.NET Core components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied