Search code examples
c#pdf-generationadobe-reader

Creating pdf from byte array


I am trying to create a pdf file from a byte array as

string str = "Hello World";
byte[] byteArray = Encoding.ASCII.GetBytes(str);
using(FileStream fs = new FileStream(path, FileMode.OpenorCreate, FileAccess.ReadWrite, FileShare.None))
 {
     fs.Write(byteArray, 0, byteArray.Length);
 }

It creates the file. However, when I try to open the file, I get the following error:

adobe reader could not open file because it is not a supported file type

I can open other pdf files just fine. What is causing this error?


Solution

  • If you want to create a Hello World file in PDF, you need a library to do so.

    For instance: this Java HelloWorld example creates this hello.pdf. Download hello.pdf and open it in a text editor, and you'll see that it contains much more than the bytes "Hello World".

    I see that you're a C# programmer. You can find the C# port of the HelloWorld example here. iText is only one of the many libraries, I'm mentioning it because I'm the original developer of iText. A simple search for PDF libraries will reveal more options.