Reputation: 93
I'm trying to load a pdf file to byte array, but after the code runs the byte array length is zero. Not sure what I'm doing wrong. The project is vs 2010, running win 7 32 bit os in vm 9x. Hopefully there is a simple fix to this problem. Thanks in advance.
Demonstrates the code I'm using to stream the pdf file to byte array
Upvotes: 0
Views: 2433
Reputation: 93
The following code worked for me:
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);
}
}
Upvotes: 1