Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show PDF from SQL Server database using C#

I want to display a .pdf that I have already uploaded into my SQL Server database, but I need it to be shown in the form and directly from the database (without saving it into my computer).

I'm using SQL Server 2012 and Visual Studio 2019.

I tried to used AxAcroPdf, but I don't know how it works.

DataTable dt = new DataTable();
SqlCommand show = new SqlCommand("SELECT documento FROM table WHERE p = '" + contentP + "'AND n = '" + contentN + "' AND documento is not null;", con);
SqlDataAdapter adapter = new SqlDataAdapter(show);
adapter.Fill(dt);

if (dt.Rows.Count > 0)
{
   byte[] ap = (byte[])dt.Rows[0]["documento"];
   MemoryStream ms = new MemoryStream(ap);

   var axacropdf = new AcroPDFLib.AcroPDF();
   axacropdf.LoadFile(ap.ToString());
   axacropdf.setShowToolbar(true);
   axacropdf.setView("Fit");
}
like image 733
luciacar Avatar asked Dec 17 '25 13:12

luciacar


1 Answers

It is better not to use adobe reader for this purpose since

  1. It has to be installed on the client PC
  2. It is a COM automation server component which is extremely slow

Instead, use nuget to get this: https://github.com/pvginkel/PdfiumViewer package.

like image 164
alexrait Avatar answered Dec 19 '25 04:12

alexrait



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!