2015-11-15 81 views
0

我想加载一个pdf文件到字节数组,但代码运行后,字节数组长度为零。不知道我做错了什么。该项目是vs 2010,在vm 9x上运行赢得7 32位操作系统。希望这个问题有一个简单的解决方法。提前致谢。流PDF到字节数组

Demonstrates the code I'm using to stream the pdf file to byte array

+0

也许路径是不正确的。文件名中有空格。 –

+0

路径和文件字符串都是正确的。问题很简单,文件是空的。我在原来的帖子后发现了这个,并且有点不好意思承认网站。 – Paul

+0

路径是正确的。我所引用的文件竟然是空的。最后要分享的是,pdf查看器上有一个属性,需要设置文档属性才能使其正常工作。渲染器的工作原理如下所示,但观看者具有的附加特性使其更适合使用,因此效果更好。谢谢您的帮助... – Paul

回答

1

下面的代码为我工作:

private void btnOpenFile_Click(object sender, EventArgs e) 
    { 
     string strId = gridViewMain.SelectedRows[0].Cells["id"].Value.ToString(); 

     if (!string.IsNullOrEmpty(strId)) 
     { 

      SqlCommand cmd = new SqlCommand(strQuery_GetAttachmentById, objConn); 
      cmd.Parameters.AddWithValue("@attachId", strId); 

      SqlDataAdapter da = new SqlDataAdapter(cmd); 

      DataTable dt = new DataTable(); 

      da.MissingSchemaAction = MissingSchemaAction.AddWithKey; 
      SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(da); 

      da.Fill(dt); 

      DataRow dr = dt.Rows[0]; 

      byte[] PDFBytes = (byte[])dr["attachment"]; 
      LoadPdf(PDFBytes); 
     } 

    } 

    public void LoadPdf(byte[] pdfBytes) 
    { 
     var PDFstream = new MemoryStream(pdfBytes); 
     LoadPdf(PDFstream); 
    } 

    public void LoadPdf(Stream pdfstream) 
    { 
     // Create PDF Document 
     var pdfDocument = PdfDocument.Load(pdfstream); 

     // Load PDF Document into WinForms Control 
     pdfRenderer1.Load(pdfDocument);  
    } 
}