Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert binary data to a pdf file

Tags:

c#

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?

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);
}
like image 347
Mario_PT Avatar asked Jan 01 '13 14:01

Mario_PT


People also ask

Are PDF files binary files?

General conventionsPDF files are either 8-bit binary files or 7-bit ASCII text files (using ASCII-85 encoding). Every line in a PDF can contain up to 255 characters.

How do I convert code to PDF?

How to convert HTML pages into PDF files: On a Windows computer, open an HTML web page in Internet Explorer, Google Chrome, or Firefox. On a Mac, open an HTML web page in Firefox. Click the “Convert to PDF” button in the Adobe PDF toolbar to start the PDF conversion.

Are PDF files ascii or binary?

A PDF file is organized using ASCII characters, except for certain elements that may have binary content. The file starts with a header containing a magic number (as a readable string) and the version of the format, for example %PDF-1.7 .


1 Answers

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.

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.aspx if you have more questions.

like image 198
David Avatar answered Oct 06 '22 09:10

David