C# 将二进制数据转换为pdf文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14111442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert binary data to a pdf file
提问by Mario_PT
I am trying to convert a binary data to its original format ".PDF," but either of the solutions I have braek my hed. The first is a little one, it creates a PDF file but it appears empty. The second one also creates a PDF file, but I can't open it. Where is the error?
我正在尝试将二进制数据转换为其原始格式“.PDF”,但两种解决方案中的任何一种都让我大吃一惊。第一个是一个小文件,它创建了一个 PDF 文件,但它看起来是空的。第二个也创建了一个 PDF 文件,但我无法打开它。错误在哪里?
First code:
第一个代码:
Conn.Open();
SqlCommand cmd = Conn.CreateCommand();
cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo ='" + id + "')";
byte[] binaryData = (byte[])cmd.ExecuteScalar();
string s = Encoding.UTF8.GetString(binaryData);
File.WriteAllText("algo.pdf", s);
Second code:
第二个代码:
Conn.Open();
SqlCommand cmd = Conn.CreateCommand();
cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo ='" + id + "')";
byte[] binaryData = (byte[])cmd.ExecuteScalar();
// Convert the binary input into Base64 UUEncoded output.
string base64String;
try
{
base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
}
catch (System.ArgumentNullException)
{
MessageBox.Show("Binary data array is null.");
return;
}
cmd.CommandText = "Select Titulo From Artigo WHERE (IDArtigo ='" + id + "')";
string titulo = (string)cmd.ExecuteScalar();
// Write the UUEncoded version to the output file.
System.IO.StreamWriter outFile;
try
{
outFile = new StreamWriter(titulo + ".pdf", false, System.Text.Encoding.ASCII);
outFile.Write(base64String);
outFile.Close();
}
catch (System.Exception exp)
{
System.Console.WriteLine("{0}", exp.Message);
}
采纳答案by David
You are writing the file as text, but you should be writing the raw bytes. A .PDF file is a binary file, not a text file, so in effect, you're filling it with the wrong data in your first code sample.
您将文件作为文本写入,但您应该写入原始字节。.PDF 文件是二进制文件,而不是文本文件,因此实际上,您在第一个代码示例中填充了错误的数据。
Try
尝试
Conn.Open();
SqlCommand cmd = Conn.CreateCommand();
cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo = @id)";
cmd.Parameters.AddWithValue("@id", id);
byte[] binaryData = (byte[])cmd.ExecuteScalar();
File.WriteAllBytes(("algo.pdf", binaryData);
string s = Encoding.UTF8.GetString(binaryData);
System.IO.File.WriteAllBytes is documented at http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspxif you have more questions.
如果您有更多问题,System.IO.File.WriteAllBytes 记录在http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx。
回答by CubeSchrauber
Try File.WriteAllBytes with the original data (binaryData) and do not try to convert it to anything else
使用原始数据 (binaryData) 尝试 File.WriteAllBytes,不要尝试将其转换为其他任何数据
回答by Sulyman
string sql = "select Lsn_File from Lessons_tbl where Lsn_name = '"TextBox1.Text + "'";
cmd = new SqlCommand(sql, dal.cn());
dal.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
byte[] lesson = (byte[])(dr[0]);
spathfile = System.IO.Path.GetTempFileName();
//move from soures to destination the extension until now is .temp
System.IO.File.Move(spathfile,
System.IO.Path.ChangeExtension(spathfile,".pdf"));
//make it real pdf file extension .pdf
spathfile = System.IO.Path.ChangeExtension(spathfile, ".pdf");
System.IO.File.WriteAllBytes(spathfile, lesson );
this.axAcroPDF1.LoadFile(spathfile);
dal.close();