How to load PDF document from database into PDF Viewer?
Essential JS 2 PDF Viewer
The Syncfusion® PDF Viewer in ASP.NET Core (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, 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 PDF Document from Database into PDF Viewer
PDF Viewer supports loading the PDF document from the database using the documentPath API. Refer to the following steps to get the PDF document from the database using the document name.
Step 1: Initialize the PDF Viewer control and provide the document name in the documentPath API.
<div id="target" style="display:block;height:600px"> <ejs-pdfviewer id="pdfviewer" style="height:750px" serviceUrl="/api/PdfViewer" documentPath="PDF Succinctly.pdf"></ejs-pdfviewer> </div>
Step 2: On initializing the PDF Viewer, the Load() web action method is called, and the method GetDocument() is invoked to get the PDF document from the database.
[AcceptVerbs("Post")]
[HttpPost]
[Route("api/[controller]/Load")]
// Post action for loading the PDF documents
public IActionResult Load([FromBody] Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
MemoryStream stream = new MemoryStream();
object jsonResult = new object();
if (jsonObject != null && jsonObject.ContainsKey("document"))
{
if (bool.Parse(jsonObject["isFileName"]))
{
// Get the PDF document from the database
byte[] docData = GetDocument(jsonObject["document"]);
stream = new MemoryStream(docData);
}
else
{
byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
stream = new MemoryStream(bytes);
}
}
jsonResult = pdfviewer.Load(stream, jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}
Step 3: The PDF document will be retrieved as a base64 string from the database using the document name and returned as a byte array to the Load() web action for loading in the PDF Viewer.
public byte[] GetDocument(string documentID)
{
// Provide the database connection string
string constr = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\XYZ\\Documents\\PdfList.mdf;Integrated Security=True;Connect Timeout=30";
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr);
// Query to get the PDF document from the database using the document name
var query = "select PdfDocData from DocList where PdfDocName = '" + documentID + "'";
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query);
cmd.Connection = con;
con.Open();
System.Data.SqlClient.SqlDataReader read = cmd.ExecuteReader();
read.Read();
// Retrieve the document data as a base64 string
string base64 = (string)read["PdfDocData"];
// Convert the base64 string to a byte array
byte[] byteArray = Convert.FromBase64String(base64);
return byteArray;
}
Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/DB-102492398
Modify the database connection string, name, and their entries as per your database entries.
Conclusion:
I hope you enjoyed learning about how to load PDF document from database into PDF Viewer.