简体   繁体   中英

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

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.

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:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM