سؤال

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?

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

The pdf format is more than a simple "Hello World". It contains large amounts of information about the formatting and the file itself. You'd need to look into libraries that help you create pdf files instead of writing out "Hello World". This is kind of like writing something in a txt file and opening it in photoshop - it won't be a valid file.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top