3

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");
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
luciacar
  • 51
  • 8
  • 2
    [SQL Injection alert](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - you should **not** concatenate together your SQL statements - use **parametrized queries** instead to avoid SQL injection - check out [Little Bobby Tables](http://bobby-tables.com/) – marc_s Jul 13 '21 at 16:59
  • @Cleptus I was using PDFSharp, but I am going to try that library, thank you – luciacar Jul 13 '21 at 17:25
  • 1
    @luciacar I was telling you that AcroPDF **could not** do it. PDFSharp looks like it can do it. [Check this PDFSharp answer](https://stackoverflow.com/a/1078941/2265446) – Cleptus Jul 13 '21 at 17:27

3 Answers3

1

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.

alexrait
  • 407
  • 3
  • 15
1

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

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • Thank you very much for the answer. The problem is that my program is going to be part of a public application, so I need it to be a free open source – luciacar Jul 14 '21 at 13:54
  • 1
    @luciacar, based on my search, ceTe.DynamicPDF.Viewer.NET is a free open source. You could look at the [nuget](https://www.nuget.org/packages/ceTe.DynamicPDF.Viewer.NET/). Sentence: Flexible licensing (including royalty free) is available to meet all your needs. – Jack J Jun Jul 15 '21 at 02:41
  • I've found another pdf viewer and now I can show the pdf without having it in the computer. I'm going to publish that solution. – luciacar Jul 16 '21 at 16:29
0

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);
}
luciacar
  • 51
  • 8