簡體   English   中英

使用 C# 顯示 SQL Server 數據庫中的 PDF

[英]Show PDF from SQL Server database using C#

我想顯示我已經上傳到我的 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");
}

最好不要為此目的使用 adobe reader,因為

  1. 它必須安裝在客戶端 PC 上
  2. 它是一個非常慢的 COM 自動化服務器組件

相反,使用 nuget 來獲取這個: https : //github.com/pvginkel/PdfiumViewer包。

根據我的測試,似乎 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);
        }

結果:

在此處輸入圖片說明

我找到了解決我的問題的方法:

我安裝了 nuget Syncfusion.PdfViewer.Windows

然后我從名為PdfDocumentView的 ToolBox 添加了一個 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM