我有一个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;
}
}
上传文件后,用户已查看该文件,我已使用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组件中加载文件而不存储在位置。
答案 0 :(得分:0)
你想要的是类似LoadStream api的东西。快速搜索Acrobat SDK似乎没有任何改变。
答案 1 :(得分:0)
如果是Web应用程序,您可以将pdf文件放入网页的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();