How to Load ASP.NET Core PDF Viewer Document from URL in Server Side?
Essential JS 2 PDF Viewer
The Syncfusion® ASP.NET Core PDF Viewer (Essential JS 2) is a modern enterprise UI toolkit that has been built from the ground up to be lightweight, responsive, modular, and touch-friendly. It is also available in other frameworks such as JavaScript, Angular, ASP.NET MVC, Blazor, and React.
Refer to the following UG link for getting started with the PDF Viewer Control.
https://ej2.syncfusion.com/aspnetcore/documentation/pdfviewer/getting-started/
Load the PDF document from the database into PDF Viewer
PDF Viewer supports loading the PDF document from a URL using the documentPath API. Refer to the following steps to view the PDF document from the URL.
Step 1:
Initialize the PDF Viewer control and provide the document name in the documentPath API.
Index.cshtml
<ejs-pdfviewer id="container" style="height:600px" serviceUrl="/api/PdfViewer" DocumentPath="<PDF Document URL>"></ejs-pdfviewer> |
Step 2:
On initializing the PDF Viewer, the Load() web action method is called, and in this method, using the WebClient, the PDF document will be downloaded and returned as a byte array to load in our PDF Viewer.
PdfViewerController.cs
[AcceptVerbs("Post")] [HttpPost] [Route("api/[controller]/Load")] public IActionResult Load([FromBody] Dictionary<string, string> jsonData) { PdfRenderer pdfviewer = new PdfRenderer(_cache); MemoryStream stream = new MemoryStream();
object jsonResult = new object(); if (jsonData != null && jsonData.ContainsKey("document")) { if (bool.Parse(jsonData["isFileName"])) { string documentPath = GetDocumentPath(jsonData["document"]);
if (!string.IsNullOrEmpty(documentPath)) { byte[] bytes = System.IO.File.ReadAllBytes(documentPath); stream = new MemoryStream(bytes); } else { string fileName = jsonData["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0]; if (fileName == "http" || fileName == "https") { WebClient WebClient = new WebClient(); byte[] pdfDoc = WebClient.DownloadData(jsonData["document"]); stream = new MemoryStream(pdfDoc); } else { return this.Content(jsonData["document"] + " is not found"); }
} } else { byte[] bytes = Convert.FromBase64String(jsonData["document"]); stream = new MemoryStream(bytes); } } jsonResult = pdfviewer.Load(stream, jsonData); return Content(JsonConvert.SerializeObject(jsonResult)); } |
Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/EJ2PDFViewer-1539197914.zip
Modify the documentPath with the correct PDF Document URL.
Conclusion
I hope you enjoyed learning about how to load an ASP.NET Core PDF Viewer document from a URL on the server side.
You can refer to our ASP.NET Core PDF Viewer feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core PDF Viewer example to understand how to create and manipulate data.
For current customers, you can check out our 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 other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!