我有一個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組件中加載文件,而無需存儲在位置。
這裏有一個提示:永遠不要使用'try/catch語句(例外){拋出前;}'剛剛離開關閉try/catch塊完全。 – 2013-04-29 05:29:32