Convert string in binary format to PDF

Asked

Viewed 1,536 times

4

I would like to know how to Gero a PDF file through a string with binary values, the values should actually turn a text (according to the ASCII table) and be written in PDF.

I tried to do it this way but I was unsuccessful:

string teste = "010001010101111001111111";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(teste);

File.WriteAllBytes(@"C:/teste.pdf", bytes); 

What’s wrong with the code? The PDF is even created but nothing is written in it, the same doesn’t even open.

2 answers

1

Redeem the bytes of String binary:

public Byte[] GetBytesFromBinaryString(String binary)
{
  var list = new List<Byte>();

  for (int i = 0; i < binary.Length; i += 8)
  {
    String t = binary.Substring(i, 8);

    list.Add(Convert.ToByte(t, 2));
  }

  return list.ToArray();
}


byte[] bytes = GetBytesFromBinaryString("010001010101111001111111");

Finally write the PDF:

File.WriteAllBytes(@"C:/teste.pdf", bytes);

Response based in that reply.

  • Hello, I did this way that you suggested however the PDF does not open, Adobe Reader says that there is no support for this type of file or it was damaged. What can it be?

  • Insert this excerpt: var text = Encoding.ASCII.GetString(bytes); and post here the value of text, please.

  • The value returned was "E^ "

  • That’s what was written in binary?

  • Yes, actually it was random the number generated. But I did a test with my name "Raphael" in binary, and also did not generate the pdf with the content.

  • In debug mode, when I hover over the "File" class, 3 properties appear: ERROR_ACCESS_DENIED = 5 ERROR_INVALID_PARAMETER = 87 Getfileexinfostandard = 0 Does this have anything to do with?

  • I did a test saving as TXT and it was normal, the problem is to generate PDF

  • Got it. Ever thought about using some framework to generate PDF? Take a look at iText

Show 3 more comments

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.