Getting Started in ASP.NET Core PDF Viewer control
6 May 202224 minutes to read
This section briefly explains about how to include ASP.NET Core PDF Viewer control in your ASP.NET Core application using Visual Studio.
Prerequisites
System requirements for ASP.NET Core controls
Create ASP.NET Core web application with Razor pages
Install ASP.NET Core package in the application
Syncfusion ASP.NET Core controls are available in nuget.org. Refer to NuGet packages topic to learn more about installing NuGet packages in various OS environments. To add ASP.NET Core controls in the application, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for Syncfusion.EJ2.AspNet.Core and then install it.
The Syncfusion.EJ2.AspNet.Core NuGet package has dependencies, Newtonsoft.Json for JSON serialization and Syncfusion.Licensing for validating Syncfusion license key.
Add Syncfusion ASP.NET Core Tag Helper
Open ~/Views/_ViewImports.cshtml
file and import the Syncfusion.EJ2
TagHelper.
@addTagHelper *, Syncfusion.EJ2
Add style sheet
Checkout the Themes topic to learn different ways (CDN, NPM package, and CRG) to refer styles in ASP.NET Core application, and to have the expected appearance for Syncfusion ASP.NET Core controls. Here, the theme is referred using CDN inside the <head>
of ~/Pages/Shared/_Layout.cshtml
file as follows,
<head>
...
<!-- Syncfusion ASP.NET Core controls styles -->
<link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/20.1.55/fluent.css" />
</head>
Add script reference
In this getting started walk-through, the required scripts are referred using CDN inside the <head>
of ~/Pages/Shared/_Layout.cshtml
file as follows,
<head>
...
<!-- Syncfusion ASP.NET Core controls scripts -->
<script src="https://cdn.syncfusion.com/ej2/20.1.55/dist/ej2.min.js"></script>
</head>
Register Syncfusion Script Manager
Open ~/Pages/Shared/_Layout.cshtml
page and register the script manager
<body>
....
....
<!-- Syncfusion ASP.NET Core Script Manager -->
<ejs-scripts></ejs-scripts>
</body>
Add ASP.NET Core PDF Viewer control
Now, add the Syncfusion ASP.NET Core PDF Viewer tag helper in ~/Pages/Index.cshtml
page. You can load a PDF file in the PDF Viewer by specifying the document name in the documentPath property as below.
<ejs-pdfviewer id="pdfviewer" style="height:600px" serviceUrl="/Index" documentPath="Files/PDF_Succinctly.pdf">
</ejs-pdfviewer>
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Syncfusion.EJ2.PdfViewer;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Reflection;
namespace PDFViewerSample.Pages
{
[IgnoreAntiforgeryToken(Order = 1001)]
public class IndexModel : PageModel
{
private readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment;
private IMemoryCache _cache;
public IndexModel(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment, IMemoryCache cache)
{
_hostingEnvironment = hostingEnvironment;
_cache = cache;
}
public IActionResult OnPostLoad([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
MemoryStream stream = new MemoryStream();
var jsonObject = JsonConverterstring(responseData);
object jsonResult = new object();
if (jsonObject != null && jsonObject.ContainsKey("document"))
{
if (bool.Parse(jsonObject["isFileName"]))
{
string documentPath = GetDocumentPath(jsonObject["document"]);
if (!string.IsNullOrEmpty(documentPath))
{
byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
stream = new MemoryStream(bytes);
}
else
{
return this.Content(jsonObject["document"] + " is not found");
}
}
else
{
byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
stream = new MemoryStream(bytes);
}
}
jsonResult = pdfviewer.Load(stream, jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}
public Dictionary<string, string> JsonConverterstring(jsonObjects results)
{
Dictionary<string, object> resultObjects = new Dictionary<string, object>();
resultObjects = results.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(results, null));
var emptyObjects = (from kv in resultObjects
where kv.Value != null
select kv).ToDictionary(kv => kv.Key, kv => kv.Value);
Dictionary<string, string> jsonResult = emptyObjects.ToDictionary(k => k.Key, k => k.Value.ToString());
return jsonResult;
}
//Post action for processing the PDF documents.
public IActionResult OnPostRenderPdfPages([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
var jsonObject = JsonConverterstring(responseData);
object jsonResult = pdfviewer.GetPage(jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}
//Post action for unloading and disposing the PDF document resources
public IActionResult OnPostUnload([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
var jsonObject = JsonConverterstring(responseData);
pdfviewer.ClearCache(jsonObject);
return this.Content("Document cache is cleared");
}
//Post action for rendering the ThumbnailImages
public IActionResult OnPostRenderThumbnailImages([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
var jsonObject = JsonConverterstring(responseData);
object result = pdfviewer.GetThumbnailImages(jsonObject);
return Content(JsonConvert.SerializeObject(result));
}
//Post action for processing the bookmarks from the PDF documents
public IActionResult OnPostBookmarks([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
var jsonObject = JsonConverterstring(responseData);
object jsonResult = pdfviewer.GetBookmarks(jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}
//Post action for rendering the annotation comments
public IActionResult OnPostRenderAnnotationComments([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
var jsonObject = JsonConverterstring(responseData);
object jsonResult = pdfviewer.GetAnnotationComments(jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}
//Post action for exporting the annotations
public IActionResult OnPostExportAnnotations([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
var jsonObject = JsonConverterstring(responseData);
string jsonResult = pdfviewer.ExportAnnotation(jsonObject);
return Content(jsonResult);
}
//Post action for importing the annotations
public IActionResult OnPostImportAnnotations([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
var jsonObject = JsonConverterstring(responseData);
string jsonResult = string.Empty;
object JsonResult;
if (jsonObject != null && jsonObject.ContainsKey("fileName"))
{
string documentPath = GetDocumentPath(jsonObject["fileName"]);
if (!string.IsNullOrEmpty(documentPath))
{
jsonResult = System.IO.File.ReadAllText(documentPath);
}
else
{
return this.Content(jsonObject["document"] + " is not found");
}
}
else
{
string extension = Path.GetExtension(jsonObject["importedData"]);
if (extension != ".xfdf")
{
JsonResult = pdfviewer.ImportAnnotation(jsonObject);
return Content(JsonConvert.SerializeObject(JsonResult));
}
else
{
string documentPath = GetDocumentPath(jsonObject["importedData"]);
if (!string.IsNullOrEmpty(documentPath))
{
byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
jsonObject["importedData"] = Convert.ToBase64String(bytes);
JsonResult = pdfviewer.ImportAnnotation(jsonObject);
return Content(JsonConvert.SerializeObject(JsonResult));
}
else
{
return this.Content(jsonObject["document"] + " is not found");
}
}
}
return Content(jsonResult);
}
//Post action for downloading the PDF documents
public IActionResult OnPostDownload([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
var jsonObject = JsonConverterstring(responseData);
string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);
return Content(documentBase);
}
//Post action for printing the PDF documents
public IActionResult OnPostPrintImages([FromBody] jsonObjects responseData)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
var jsonObject = JsonConverterstring(responseData);
object pageImage = pdfviewer.GetPrintImage(jsonObject);
return Content(JsonConvert.SerializeObject(pageImage));
}
//Gets the path of the PDF document
private string GetDocumentPath(string document)
{
string documentPath = string.Empty;
if (!System.IO.File.Exists(document))
{
string basePath = _hostingEnvironment.WebRootPath;
string dataPath = string.Empty;
dataPath = basePath + "\\";
if (System.IO.File.Exists(dataPath + (document)))
documentPath = dataPath + document;
}
else
{
documentPath = document;
}
return documentPath;
}
}
public class jsonObjects
{
public string document { get; set; }
public string password { get; set; }
public int zoomFactor { get; set; }
public bool isFileName { get; set; }
public int xCoordinate { get; set; }
public int yCoordinate { get; set; }
public int pageNumber { get; set; }
public int tileXcount { get; set; }
public int tileYcount { get; set; }
public string extraText { get; set; }
public string documentId { get; set; }
public string hashId { get; set; }
public float sizeX { get; set; }
public float sizeY { get; set; }
public int startPage { get; set; }
public int endPage { get; set; }
public string stampAnnotations { get; set; }
public string textMarkupAnnotations { get; set; }
public string stickyNotesAnnotation { get; set; }
public string shapeAnnotations { get; set; }
public string measureShapeAnnotations { get; set; }
public string action { get; set; }
public int pageStartIndex { get; set; }
public int pageEndIndex { get; set; }
public string fileName { get; set; }
public string elementId { get; set; }
public string pdfAnnotation { get; set; }
public string importPageList { get; set; }
public string annotationDataFormat { get; set; }
public string uniqueId { get; set; }
public string data { get; set; }
public float viwePortWidth { get; set; }
public float viewportHeight { get; set; }
public int tilecount { get; set; }
public bool isCompletePageSizeNotReceived { get; set; }
public string freeTextAnnotation { get; set; }
public string signatureData { get; set; }
public string fieldsData { get; set; }
public string documentLiveCount { get; set; }
}
}
In this code,
ejs-pdfviewer refers to the PDFViewer control among the EJ components with id as “pdfviewer”.
serviceUrl is necessary to communicate with the server which also specifies the path of the controller. Here, PdfViewer is the name of the controller.
documentPath is the property needed to load a PDF file in the PDF Viewer. The specified document must be placed inside the folder structure that matches with the path in the GetDocumentPath(string document) method inside the PdfViewerController.
Press Ctrl+F5 (Windows) or ⌘+F5 (macOS) to run the app. Then, the Syncfusion ASP.NET Core PDF Viewer control will be rendered in the default web browser.