【问题标题】:Show PDF from SQL Server database using C#使用 C# 从 SQL Server 数据库显示 PDF
【发布时间】:2021-09-22 17:54:48
【问题描述】:

我想显示一个我已经上传到我的 SQL Server 数据库中的.pdf,但我需要它显示在表单中并直接从数据库中显示(而不是将其保存到我的计算机中)。

我使用的是 SQL Server 2012 和 Visual Studio 2019。

我尝试使用 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");
}

【问题讨论】:

  • SQL Injection alert - 您应该将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 - 查看Little Bobby Tables
  • @Cleptus 我用的是 PDFSharp,但我要试试那个库,谢谢
  • @luciacar 我告诉你 AcroPDF 不能做到这一点。 PDFSharp 看起来可以做到。 Check this PDFSharp answer

标签: c# database pdf sql-server-2012 visual-studio-2019


【解决方案1】:

最好不要使用adobe reader,因为

  1. 它必须安装在客户端 PC 上
  2. 这是一个非常慢的 COM 自动化服务器组件

改为使用 nuget 来获取: https://github.com/pvginkel/PdfiumViewer 包。

【讨论】:

  • 谢谢,我要试试那个包
【解决方案2】:

根据我的测试,AcroPDF 似乎不支持加载字节数组以在 winform 中显示 pdf。

我找到了另一种直接从数据库中显示 pdf 的方法。

首先,请尝试安装以下nuget->ceTe.DynamicPDF.Viewer.NET

其次,请从工具箱中添加一个名为pdfviewer 的控件。

第三,你可以尝试下面的代码从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);
        }

结果:

【讨论】:

  • 非常感谢您的回答。问题是我的程序将成为公共应用程序的一部分,所以我需要它是一个免费的开源程序
  • @luciacar,根据我的搜索,ceTe.DynamicPDF.Viewer.NET 是一个免费的开源。你可以看看nuget。句子:灵活的许可(包括免版税)可满足您的所有需求。
  • 我找到了另一个 pdf 查看器,现在我可以在没有计算机的情况下显示 pdf。我将发布该解决方案。
【解决方案3】:

我找到了解决问题的方法:

我安装了 nuget Syncfusion.PdfViewer.Windows

然后我从 ToolBox 添加了一个名为 PdfDocumentView 的 pdf 查看器

我放下一段代码,在开头添加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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-12
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多