我正在尝试将pdf文件加载到字节数组,但在代码运行后,字节数组长度为零。不知道我做错了什么。该项目是vs 2010,以9x的速度运行win 7 32 bit os。希望有一个简单的解决方案来解决这个问题。提前谢谢。
Demonstrates the code I'm using to stream the pdf file to byte array
答案 0 :(得分: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);
}
}