Hi rani,
Check this example. Now please take its reference and correct your code.
Install Spire.Pdf library from nuget.
Install-Package Spire.PDF -Version 8.1.0
Namespaces
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Spire.Pdf;
Controller
public class HomeController : Controller
{
private IHostingEnvironment Environment;
public HomeController(IHostingEnvironment _environment)
{
Environment = _environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(IFormFile postedFile)
{
string path = System.IO.Path.Combine(this.Environment.WebRootPath, "Uploads");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string fileName = System.IO.Path.GetFileName(postedFile.FileName);
using (FileStream stream = new FileStream(System.IO.Path.Combine(path, fileName), FileMode.Create))
{
postedFile.CopyTo(stream);
}
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(System.IO.Path.Combine(path, fileName));
List<string> images = new List<string>();
foreach (PdfPageBase page in doc.Pages)
{
foreach (Stream image in page.ExtractImages())
{
using (MemoryStream ms = new MemoryStream())
{
image.CopyTo(ms);
images.Add("data:image/png;base64," + Convert.ToBase64String(ms.ToArray(), 0, ms.ToArray().Length));
}
}
}
ViewBag.Images = images;
return View();
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form asp-action="Index" asp-controller="Home" method="post" enctype="multipart/form-data" >
<input type="file" name="postedFile" />
<input type="submit" value="Upload" />
@if (ViewBag.Images != null)
{
<hr />
foreach (string image in ViewBag.Images)
{
<img src='@image' alt="Alternate Text" />
}
}
</form>
</body>
</html>
Screenshots
PDF File
Output