Introduction
In this article, we are going to cover how to implement Document Viewer in Asp.Net Core 7.0 application using visual studio community 2022.
What is GroupDocs.Viewer ?
GroupDocs.Viewer is a document viewing and rendering library developed by GroupDocs, a company that specializes in providing .NET and Java APIs for document manipulation and conversion. GroupDocs.Viewer is designed to help developers integrate document viewing capabilities into their applications, allowing users to view various types of documents directly within the application's interface without needing to download or open them with external software.
- Let's start the building of the document viewer.
- import the bootstrap 5 cdn in _Layout.html.
Install the GroupDocs.Viewer from Nuget Package.

- Create a new ASP.NET CORE MVC project.
- Name the project: DocumentViewer.Application.
- Create a new controller in DocumentViewer.Application project.
public class DocViewerController : Controller
{
private readonly IHostingEnvironment _hostingEnvironment;
private string projectRootPath;
private string outputPath;
private string storagePath;
List<string> lstFiles;
public DocViewerController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
projectRootPath = _hostingEnvironment.ContentRootPath;
outputPath = Path.Combine(projectRootPath, "wwwroot/Content");
storagePath = Path.Combine(projectRootPath, "storage");
lstFiles = new List<string>();
}
public IActionResult Index()
{
var files = Directory.GetFiles(storagePath);
foreach (var file in files)
{
lstFiles.Add(Path.GetFileName(file));
}
ViewBag.lstFiles = lstFiles;
return View();
}
[HttpPost]
public IActionResult OnPost(string FileName)
{
int pageCount = 0;
string imageFilesFolder = Path.Combine(outputPath, Path.GetFileName(FileName).Replace(".", "_"));
if (!Directory.Exists(imageFilesFolder))
{
Directory.CreateDirectory(imageFilesFolder);
}
string imageFilesPath = Path.Combine(imageFilesFolder, "page-{0}.png");
using (Viewer viewer = new Viewer(Path.Combine(storagePath, FileName)))
{
//Get document info
ViewInfo info = viewer.GetViewInfo(ViewInfoOptions.ForPngView(false));
pageCount = info.Pages.Count;
//Set options and render document
PngViewOptions options = new PngViewOptions(imageFilesPath);
viewer.View(options);
}
return new JsonResult(pageCount);
}
}
At the heart of this code is a method marked with the [HttpPost] attribute, which indicates that it responds to HTTP POST requests. The method takes a parameter called FileName, representing the name of the uploaded file. The method returns an IActionResult, allowing various types of responses to be returned, such as JSON, views, or redirects.



Join the conversation! Your thoughts help the community grow.