将二进制数据转换为PDF文件

5
我将尝试将二进制数据转换为其原始格式".PDF",但是我现有的两个解决方案都出现了问题。第一个解决方案创建了一个PDF文件,但该文件为空。第二个解决方案也创建了一个PDF文件,但我无法打开它。错误在哪里?
第一个代码:
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);

第二段代码:
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);
}

如果您想从字节数组创建PDF,可以查看这个链接 - Tilak
3个回答

10

你正在以文本形式编写文件,但实际上应该以原始字节形式编写。PDF文件是二进制文件,而不是文本文件,因此,在你的第一个代码示例中,你填充它的数据是错误的。

尝试

    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);

如需更多信息,请参阅http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx上的文档。


2
这是我在多个问题和搜索中找到的最佳答案。我想建议将此命令文本参数化,而不是连接字符串。即使您的id变量设置为int并且您不关心安全性,出于一致性考虑,这仍然是一个好习惯。 - Brad

3
尝试使用File.WriteAllBytes处理原始数据(binaryData),不要尝试将其转换为其他格式。

0
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();

1
请仅返回翻译文本:请勿提供类似于 "select Lsn_File from Lessons_tbl where Lsn_name = '"TextBox1.Text + "'"; 的代码,因为这样容易遭受 SQL 注入攻击。 - Mathias
@Mathias:您可能想查看关于在回答中编辑安全漏洞修复的此元问题 - BDL
@BDL:在相关问题中,它说:“修复答案中的潜在安全漏洞是一个好的编辑。”我请求了一次编辑,但被拒绝了。如果我有什么不对的地方,我很抱歉。应该如何处理这种情况? - Mathias
应该说得更清楚一些:我开了这个元问题是因为你的一次编辑。那是一个持续的讨论,我想给你参与的机会。 - BDL
哦,抱歉没看到。 - Mathias

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接