2013-04-29 14 views
-2

我有一個pdf文件,在admin中所有的pdf文件都轉換成存儲在數據庫中的字節數組。在windows應用程序的adobe閱讀器組件中加載字節數組到pdf中

public void Getfile() 
{ 
    byte[] file; 
    string varFilePath = @"D:\PDFConvertion\pdf\100CountryHouses.pdf"; 
    try 
    { 
     using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read)) 
     { 
      using (var reader = new BinaryReader(stream)) 
      { 
       file = reader.ReadBytes((int)stream.Length); 
      } 
     } 
     SqlConnection sqlCon; 
     sqlCon = new SqlConnection("server details");    
     sqlCon.Open(); 
     using (var sqlWrite = new SqlCommand("INSERT INTO pdftest Values(@File)", sqlCon)) 
     { 
      sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file; 
      sqlWrite.ExecuteNonQuery(); 
     } 
     sqlCon.Close(); 
     MessageBox.Show("Converted Success - Length " + Convert.ToString(file.Length)); 
    } 
    catch (Exception Ex) 
    { 
     throw Ex; 
    } 
} 

一旦文件已上傳用戶已查看該文件,我已經使用Adobe Reader組件加載PDF文件。

 private void button1_Click(object sender, EventArgs e) 
     { 
      string varPathToNewLocation = @"D:\PDFConvertion\converted\test.pdf"; 
      try 
      { 
       SqlConnection sqlCon; 
       sqlCon = new SqlConnection(""); 
       sqlCon.Open(); 
       using (var sqlQuery = new SqlCommand(@"SELECT test FROM [dbo].[pdftest] ", sqlCon)) 
       { 

        using (var sqlQueryResult = sqlQuery.ExecuteReader()) 
         if (sqlQueryResult != null) 
         { 
          sqlQueryResult.Read(); 
          var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))]; 
          sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length); 
          using (var fs = new FileStream(varPathToNewLocation, FileMode.Create, FileAccess.Write)) 
           fs.Write(blob, 0, blob.Length); 
         } 
       } 
       sqlCon.Close(); 
       axAcroPDF1.LoadFile(@"D:PDFConvertion\converted\test.pdf"); 

      } 
      catch (Exception Ex) 
      { 
       throw Ex; 
      } 
     } 

但我想直接在Adobe Reader組件中加載文件,而無需存儲在位置。

+0

這裏有一個提示:永遠不要使用'try/catch語句(例外){拋出前;}'剛剛離開關閉try/catch塊完全。 – 2013-04-29 05:29:32

回答

0

你想要的就像是一個LoadStream api。 Acrobat SDK的快速搜索似乎沒有任何結果。

0

如果它是一個Web應用程序,您可以將PDF文件放入網頁的Response對象中。然後瀏覽器自動打開acrobat reader。下面是一個C#代碼段:

Response.Buffer = true; 
Response.Clear(); 
Response.ContentType = "application/pdf"; 

// if blob is the byte array of the pdf file 
Response.BinaryWrite(blob); 
Response.Flush(); 
Response.End(); 
+0

不是它的一個非web應用程序。 – LogeshP 2013-04-29 07:19:55

相關問題