Category / Section
How to load the base64 string in PDF Viewer
1 min read
Load base64 to PDF Viewer
PDF Viewer supports loading the PDF document from database as base64 string using the documentPath API.
The following code example will get the PDF document as byte array using its name from the database and converts the same into base64 string.
public object GetDocument() { string documentID = "Xyz.pdf"; string constr = System.Configuration.ConfigurationManager.ConnectionStrings["PdfDocument"].ConnectionString; System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr); //Searches for the PDF document from the database var query = "select Data from PdfDocuments where DocumentName = '" + 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(); //Reads the PDF document data as byte array from the database byte[] byteArray = (byte[])read["Data"]; //Converts byte array into base64 string return "data:application/pdf;base64," + Convert.ToBase64String(byteArray); }
Note:
Please modify the database name and their entries as per your database entries.
Refer to the sample to load the base64 string in PDF Viewer.
https://www.syncfusion.com/downloads/support/directtrac/general/ze/EJ2PdfViewer_Core-135324210
In the sample, clicking the ‘Load PDF document as Base64String’ will get PDF document from the local disk as base64 string in GetDocument() and load it in PDF Viewer using documentPath.