在windows应用程序的adobereader组件中加载字节数组为pdf
本文关键字:字节数 字节 数组 pdf 加载 应用程序 windows adobereader 组件 | 更新日期: 2023-09-27 18:11:29
我有一个pdf文件,在admin中所有的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;
}
}
一旦文件上传,用户已经查看了文件,我已经使用adobereader组件加载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;
}
}
但是我想在adobereader组件中直接加载文件,而不需要存储在位置。
你想要的是类似LoadStream api的东西。快速搜索一下Acrobat SDK似乎没有找到任何东西
如果是web应用程序,可以将pdf文件放入web页面的Response对象中。然后浏览器自动打开acrobat阅读器。下面是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();