luciacar
luciacar

Reputation: 51

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");
}

Upvotes: 3

Views: 2877

Answers (3)

luciacar
luciacar

Reputation: 51

I've found a solution for my problem:

I installed the nuget Syncfusion.PdfViewer.Windows

Then I added a pdf viewer from the ToolBox called PdfDocumentView

And I put the next code, adding the SqlConnection at the beginning:

DataTable dt = new DataTable();
SqlCommand verDoc = new SqlCommand("SELECT documento FROM inspeccionGestionDocumental WHERE placa = '" + contenidoPlaca + "'AND numeroPropuesta = '" + contenidoNumeroPropuesta + "' AND documento is not null;", con);
SqlDataAdapter adapter = new SqlDataAdapter(consultar);
adapter.Fill(dt);

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

Upvotes: 0

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

Based on my test, It seems that AcroPDF doesn't support load the byte array to show the pdf in winform.

I find another method to show pdf from the database directly.

First, please try to install the following nuget->ceTe.DynamicPDF.Viewer.NET.

Second, please add a control called pdfviewer from the Toolbox.

Third, you can try the following code to load the pdf from the byte array in winform.

        string connstr = "connstr";
        SqlConnection connection = new SqlConnection(connstr);
        connection.Open();
        string sql = "select * from PdfReader";
        SqlCommand cmd = new SqlCommand(sql, connection);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataTable table = new DataTable();
        adapter.Fill(table);
        if (table.Rows.Count > 0)
        {
            byte[] ap = (byte[])table.Rows[0]["Content"];
            PdfDocument pdfDocument = new PdfDocument(ap);
            pdfViewer1.Open(pdfDocument);
        }

Result:

enter image description here

Upvotes: 1

alexrait
alexrait

Reputation: 417

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.

Upvotes: 1

Related Questions