简体   繁体   English

使用 C# 显示 SQL Server 数据库中的 PDF

[英]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).我想显示我已经上传到我的 SQL Server 数据库中的.pdf ,但我需要将它显示在表单中并直接从数据库中显示(无需将其保存到我的计算机中)。

I'm using SQL Server 2012 and Visual Studio 2019.我正在使用 SQL Server 2012 和 Visual Studio 2019。

I tried to used AxAcroPdf, but I don't know how it works.我尝试使用 AxAcroPdf,但我不知道它是如何工作的。

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最好不要为此目的使用 adobe reader,因为

  1. It has to be installed on the client PC它必须安装在客户端 PC 上
  2. It is a COM automation server component which is extremely slow它是一个非常慢的 COM 自动化服务器组件

Instead, use nuget to get this: https://github.com/pvginkel/PdfiumViewer package.相反,使用 nuget 来获取这个: https : //github.com/pvginkel/PdfiumViewer包。

Based on my test, It seems that AcroPDF doesn't support load the byte array to show the pdf in winform.根据我的测试,似乎 AcroPDF 不支持加载字节数组以在 winform 中显示 pdf。

I find another method to show pdf from the database directly.我找到了另一种直接从数据库显示pdf的方法。

First, please try to install the following nuget-> ceTe.DynamicPDF.Viewer.NET .首先,请尝试安装以下 nuget-> ceTe.DynamicPDF.Viewer.NET

Second, please add a control called pdfviewer from the Toolbox.其次,请从工具箱中添加一个名为pdfviewer的控件。

Third, you can try the following code to load the pdf from the byte array in winform.第三,可以试试下面的代码,从winform中的字节数组中加载pdf。

        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我安装了 nuget Syncfusion.PdfViewer.Windows

Then I added a pdf viewer from the ToolBox called PdfDocumentView然后我从名为PdfDocumentView的 ToolBox 添加了一个 pdf 查看器

And I put the next code, adding the SqlConnection at the beginning:然后我放了下一个代码,在开头添加了 SqlConnection:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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