We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Use pdf viewer with a document stored in a database

Thread ID:

Created:

Updated:

Platform:

Replies:

142423 Feb 2,2019 06:07 PM UTC Feb 6,2019 06:43 AM UTC ASP.NET MVC - EJ 2 3
loading
Tags: PDF Viewer
Guillermo Goberna
Asked On February 2, 2019 06:07 PM UTC

I would like to use the pdf viewer 

 @Html.EJS().PdfViewer("pdfviewer").ServiceUrl(VirtualPathUtility.ToAbsolute("~/PdfViewer/")).Render()

But the document is not stored in a folder in the webserver, it is stored in a database, so I need to retrieve the document from the database (not a problem) and load the document in the pdfviewer component.

Could you provide an example? 

Thank you.

I'm using the controller and razor view from your website: 

https://ej2.syncfusion.com/aspnetmvc/documentation/pdfviewer/getting-started/



Ramya Thirugnanam [Syncfusion]
Replied On February 4, 2019 12:22 PM UTC

Hi Guillermo, 
 
Thanks for contacting Syncfusion support. 
  
PDF viewer supports to load the PDF document from database as base64 string using the documentPath API.  
  
The below 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: Kindly modify the Database name and their entries as per your database entries. 
  
We have created the sample to load the base64 string in PDF viewer and shared the same in the following link,  
  
  
In the preceding sample, clicking on 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. Similarly, you can get the PDF document from database and load it into the PDF Viewer. 
  
Regards, 
Ramya T 


Guillermo Goberna
Replied On February 6, 2019 06:25 AM UTC

Thank you very much,

I left here also the code in case is useful for anyone else: 

View: 
<div style="height:800px;width:100%;">
       @Html.EJS().PdfViewer("pdfviewer").ServiceUrl(VirtualPathUtility.ToAbsolute("~/PDF/")).DocumentPath(ViewBag.Documento).Render()
</div>

Controller:
        public ActionResult Visor(int id)
        {
            // Reads the PDF document data as byte array from the database (DBtm is the dbml class that connects to the Documents database)
            byte[] byteArray = DBtm.Documents.First(x => x.IdDocument == id).Document.ToArray();

            //converts byte array into base64 string
            ViewBag.Documento = "data:application/pdf;base64," + Convert.ToBase64String(byteArray);
            return View();
        }

Ramya Thirugnanam [Syncfusion]
Replied On February 6, 2019 06:43 AM UTC

Hi Guillermo,  
 
Thanks for your update. 
 
Regards,   
Ramya T   


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;